From 7571ceac23025dd24726b8bb86343526a5d097f4 Mon Sep 17 00:00:00 2001 From: Ravindra Barthwal Date: Wed, 21 Aug 2024 13:30:02 +0530 Subject: [PATCH] feat: Added method to delete expired cache in cache_manager.dart - Added deleteExpiredCache() method - Removed unused _instance field from the cache_manager.dart --- lib/src/cache_manager.dart | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/src/cache_manager.dart b/lib/src/cache_manager.dart index b0becbe..c9cc4d7 100644 --- a/lib/src/cache_manager.dart +++ b/lib/src/cache_manager.dart @@ -10,7 +10,6 @@ typedef ExpirationCallback = void Function(String key); /// CacheManager class to handle caching operations with optional TTL (Time-To-Live). /// It also supports encryption, backup, and restore functionalities. class CacheManager { - static final CacheManager _instance = CacheManager._internal(); static Database? _database; final encrypt.Encrypter _encrypter = encrypt.Encrypter(encrypt.AES(encrypt.Key.fromLength(32))); @@ -130,6 +129,16 @@ class CacheManager { await db.delete('cache'); } + /// Deletes all the expired cache entries based on ttl. + /// + /// Returns the number of entries deleted. + Future deleteExpiredCache() async { + final db = await database; + return await db.delete('cache', + where: 'ttl != NULL && ttl < ?', + whereArgs: [DateTime.now().millisecondsSinceEpoch]); + } + /// Backs up cache data to a specified file path. Future backupCache(String backupPath) async { final db = await database;