LRU storage
LRU storage provides two storage methods based on the Least Recently Used (LRU) eviction policy: in-memory caching and persistent disk caching.
-
Memory cache (APLRUMemoryCache): A thread-safe in-memory LRU cache for ID objects, implemented with a linked list for high efficiency.
-
Disk cache (APLRUDiskCache): A persistent LRU cache backed by a database for objects that support the NSCoding protocol. Database-backed storage simplifies maintenance and avoids cluttering the disk with files.
Memory cache
-
@property (nonatomic, assign) BOOL handleMemoryWarning; // default NO
Specifies whether to handle system memory warnings. Default: NO. If set to YES, the cache is cleared when a memory warning occurs.
-
(id)initWithCapacity:(NSInteger)capacity;
Initializes the cache with a specified capacity.
-
(void)setObject:(id)object forKey:(NSString*)key;
Stores an object in the cache. If the object is nil, the existing object for the key is deleted.
-
(void)setObject:(id)object forKey:(NSString*)key expire:(NSTimeInterval)expire;
Stores an object in the cache with a specified expiration timestamp.
-
(id)objectForKey:(NSString*)key;
Retrieves an object.
-
(void)removeObjectForKey:(NSString*)key;
Deletes an object.
-
(void)removeAllObjects;
Deletes all objects.
-
(void)addObjects:(NSDictionary*)objects;
Adds objects in a batch. You cannot set an individual expiration time for each object. By default, objects never expire.
-
(void)removeObjectsWithRegex:(NSString*)regex;
Deletes objects in a batch where the key matches the specified regular expression.
-
(void)removeObjectsWithPrefix:(NSString*)prefix;
Deletes all objects whose keys have a specific prefix.
-
(void)removeObjectsWithSuffix:(NSString*)suffix;
Deletes all objects whose keys have a specific suffix.
-
(void)removeObjectsWithKeys:(NSSet*)keys;
Deletes all objects specified by the set of keys.
-
(NSArray*)peekObjects:(NSInteger)count fromHead:(BOOL)fromHead;
Returns an array of cached objects without applying the LRU policy. If `fromHead` is YES, the traversal starts from the head. Otherwise, the traversal starts from the tail.
-
(BOOL)objectExistsForKey:(NSString*)key;
Checks whether an object for a specific key exists without affecting the LRU order.
-
(void)resetCapacity:(NSInteger)capacity;
Updates the capacity. If the new capacity is smaller than the current capacity, excess cached objects are removed.
Disk cache
-
(id)initWithName:(NSString*)name capacity:(NSInteger)capacity userDependent:(BOOL)userDependent crypted:(BOOL)crypted;
Creates a persistent LRU cache. Stored objects must support the NSCoding protocol.
Parameters:
Parameter
Description
name
The name of the cache. This is used as the database table name.
capacity
The capacity. The actual capacity may be slightly larger to improve performance when adding data to a full cache.
userDependent
Specifies whether the cache is user-specific. If it is, the cache cannot be accessed when
APDataCenter.currentUserIdis empty. When you switch users, the cache automatically points to the current user's table without requiring any additional handling.crypted
Specifies whether the data is encrypted.
Return value: The cache instance, which your application must retain.
-
(void)setObject:(id)object forKey:(NSString*)key;
Caches an object that never expires. This is equivalent to calling `setObject:forKey:expire:` with an `expire` value of 0.
-
(void)setObject:(id)object forKey:(NSString*)key expire:(NSTimeInterval)expire;
Caches an object with a specified expiration timestamp.
Parameters:
Parameter
Description
object
The object. If nil, the object for the specified key is deleted.
key
-
expire
The expiration timestamp. Specify an absolute timestamp relative to 1970. You can use `[date timeIntervalSince1970]`.
-
(id)objectForKey:(NSString*)key;
Retrieves an object. If the object has expired, it is deleted and nil is returned. This method waits for any concurrent `setObject` operations to complete before proceeding.
-
(void)removeObjectForKey:(NSString*)key;
Deletes an object.
-
(void)removeAllObjects;
Deletes all objects.
-
(void)addObjects:(NSDictionary*)objects;
Adds objects in a batch.
-
(void)removeObjectsWithSqlLike:(NSString*)like;
Deletes objects in a batch where the key matches a SQLite `LIKE` statement.
-
(void)removeObjectsWithKeys:(NSSet*)keys;
Deletes all objects specified by the set of keys.