A lightweight C# database library with LRU caching and thread-safe operations.
License: GNU
Author: ItzKITb
Main namespace containing all database classes.
Internal utility class handling cache, locks, and debug settings.
public class Worker
{
public static LruCache<string, CacheItem> cache = new(1000);
internal static ConcurrentDictionary<string, SemaphoreSlim> file_locks = new();
public static bool debug = false; // Enable debug logging
internal static DateTime start = DateTime.UtcNow;
}Tracks database operations globally.
public class Statistics
{
public static BigInteger reads { get; } // Total file reads
public static BigInteger writes { get; } // Total file writes
public static BigInteger cache_reads { get; } // Total cache hits
public static BigInteger cache_writes { get; } // Total cache updates
public static BigInteger removes { get; } // Total key deletions
public static BigInteger renames { get; } // Total key renames
public static BigInteger checks { get; } // Total file existence checks
}Thread-safe synchronous database operations.
public static void CreateDatabase(string path)- Creates an empty JSON database file if it doesn't exist.
public static void Save(string path, string key, object data)- Saves/updates serialized
dataunderkeyin the specified database file.
public static T Get<T>(string path, string key)- Retrieves and deserializes data from
keyin the database. - Returns
default(T)if key doesn't exist.
public static void DeleteKey(string path, string key)- Removes a key from the database.
public static bool IsContainsKey(string path, string key)- Checks if a key exists in the database.
public static void RenameKey(string path, string oldKey, string newKey)- Renames a database key.
Asynchronous counterpart of Manager with Task-based methods.
CreateDatabaseSaveGet<T>DeleteKeyIsContainsKeyRenameKey
LRU (Least Recently Used) cache implementation.
public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
public async Task<TValue> GetOrAddAsync(...)
public bool TryGet(TKey key, out TValue value)
public void AddOrUpdate(TKey key, TValue value)
public void Invalidate(TKey key)capacity: Maximum cached itemscount: Current cached items
public class CacheItem
{
public Dictionary<string, JsonElement> data { get; set; }
public DateTime last_modified { get; set; }
}- Internal cache item format storing JSON data and modification timestamp.
Enable debug output via Worker.debug = true;.
Messages follow format:
[DankDB debug] [<microseconds since start>]: <message>
// Initialize database
Manager.CreateDatabase("data.db");
// Sync write/read
Manager.Save("data.db", "user1", new { Name = "Alice", Age = 30 });
var user = Manager.Get<User>("data.db", "user1");
// Async write/read
await AsyncManager.SaveAsync("data.db", "user2", new { Name = "Bob", Age = 25 });
var user2 = await AsyncManager.GetAsync<User>("data.db", "user2");
// Check statistics
Console.WriteLine($"Total writes: {Statistics.writes}");- Thread Safety: Uses
SemaphoreSlimper file path for concurrency control. - Caching: LRU cache (default: 1000 items (To change, use Worker.cache = new({Number of items the cache can store});)) with file timestamp validation.
- Serialization: Uses
System.Text.Jsonfor JSON operations.