NAME
Data::SimpleKV - A simple key-value database with memory cache and disk persistence
SYNOPSIS
use Data::SimpleKV;
my $db = Data::SimpleKV->new(
db_name => 'myapp',
data_dir => '/var/lib/simplekv' # optional
);
$db->set('key1', '测试值');
my $value = $db->get('key1');
my $exists = $db->exists('key1');
$db->delete('key1');
$db->save();
DESCRIPTION
Data::SimpleKV provides a simple key-value database with in-memory caching and disk persistence. It supports UTF-8 data storage and is safe for multi-process usage.
get($key)
Get value by key. Returns undef if key doesn't exist.
set($key, $value)
Set key-value pair.
delete($key)
Delete key-value pair. Returns 1 if key existed, 0 otherwise.
exists($key)
Check if key exists. Returns 1 if exists, 0 otherwise.
save()
Save data to disk. This method is process-safe using file locking.
keys()
Get all keys as a list.
clear()
Clear all data from memory cache.
FILE STRUCTURE
The module stores data files in the following locations:
Primary location: /var/lib/simplekv/
Fallback location: $HOME/.simplekv/
Emergency fallback: /tmp/.simplekv/
Files created:
{db_name}.db - The main data file (Storable binary format)
File permissions are set to 0644 (readable by all, writable by owner).
MULTI-PROCESS SAFETY
The module uses file locking to ensure safe concurrent access:
Exclusive locks for writing during save operations
Lock files prevent concurrent write operations
Note: Data merging between processes is not supported. The last process to call save() will overwrite previous changes.
UTF-8 SUPPORT
The module fully supports UTF-8 encoded strings for both keys and values. All string data is properly encoded/decoded to ensure correct storage and retrieval of international characters.
DEPENDENCIES
Storable - For binary serialization
Fcntl - For file locking
File::Path - For directory creation
File::Spec - For cross-platform file paths
Encode - For UTF-8 handling
AUTHOR
Y Peng, <ypeng at t-online.de>
LICENSE AND COPYRIGHT
This software is Copyright (c) 2025 by Y Peng.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)