All Products
Search
Document Center

LRU storage

Last Updated: May 18, 2021

Based on LRU elimination rule, LRU storage provides the following two storage methods:

  • Memory cache (APLRUMemoryCache): Provide the memory cache based on LRU elimination algorithm to cache ID objects. APLRUMemoryCache is thread-safe. In addition, LRU algorithm is implemented based on linked list with high efficiency.
  • Disk cache (APLRUDiskCache): Provide the LRU elimination algorithm-based cache that is persisted to databases, and caches objects that support NSCoding. It is easier to maintain databases than files, and using databases makes the disk cleaner.

Memory cache

  • @property (nonatomic, assign) BOOL handleMemoryWarning; // default NO

    Set whether to handle the system memory warning, NO by default. If it is YES, when a memory warning appears, the system will clear the cache.

  • (id)initWithCapacity:(NSInteger)capacity;

    Initialize and specify the capacity.

  • (void)setObject:(id)object forKey:(NSString*)key;

    Store the object into cache. If the object is nil, it will be deleted.

  • (void)setObject:(id)object forKey:(NSString*)key expire:(NSTimeInterval)expire;

    Store the object into cache, and specify a expiration timestamp.

  • (id)objectForKey:(NSString*)key;

    Get objects.

  • (void)removeObjectForKey:(NSString*)key;

    Delete objects.

  • (void)removeAllObjects;

    Delete all objects.

  • (void)addObjects:(NSDictionary*)objects;

    Add data in batch. It is unable to set the expiration time of every object. By default, all objects will never expire.

  • (void)removeObjectsWithRegex:(NSString*)regex;

    Delete data in batch. The key of data matches with the regular expression.

  • (void)removeObjectsWithPrefix:(NSString*)prefix;

    Delete the data that have a specific prefix in batch.

  • (void)removeObjectsWithSuffix:(NSString*)suffix;

    Delete the data that have a specific suffix in batch.

  • (void)removeObjectsWithKeys:(NSSet*)keys;

    Delete all the data that correspond to the keys in batch.

  • (NSArray*)peekObjects:(NSInteger)count fromHead:(BOOL)fromHead;

    Read the cached objects into an array, but not apply the LRU cache strategy on them. When fromHead is YES, the system traverses from start to end; otherwise, the end-to-start traversal is performed.

  • (BOOL)objectExistsForKey:(NSString*)key;

    Fast determine if the object of a key exists or not, without any influence on LRU.

  • (void)resetCapacity:(NSInteger)capacity;

    Reset capacity. If the new capacity is smaller than the original capacity, partial cache will be deleted.

Disk cache

  • (id)initWithName:(NSString*)name capacity:(NSInteger)capacity userDependent:(BOOL)userDependent crypted:(BOOL)crypted;

    Create a persistent LRU cache, and the stored objects must support NsSCoding protocol.

    Parameter

    Parameter Description
    name Cache name, which is used as the table name in the database.
    capacity Capacity (the actual capacity is larger than it), which is used to solve the performance problem on adding data when the cache is full.
    userDependent Whether it is related to the user; if yes, when the APDataCenter.currentUserId is null, the cache doesn’t work. After user switching, the cache automatically points to the current user’s table, the business doesn’t necessarily care this event.
    crypted Whether the data are encrypted.

    Return value

    Cache instance, required for the business.

  • (void)setObject:(id)object forKey:(NSString*)key;

    Cache an object, and the expire defaults to 0, namely the object will never expire.

  • (void)setObject:(id)object forKey:(NSString*)key expire:(NSTimeInterval)expire;

    Cache an object, and specify a expiration timestamp.

    Parameter

    Parameter Description
    object Object, if it is nil, the object of the specified key will be deleted.
    key key
    expire Expiration timestamp, specify an absolute timestamp relative to 1970 You can use [date timeIntervalSince1970].
  • (id)objectForKey:(NSString*)key;

    Get objects. If the specified expiration timestamp has been up when the objects are got, the system will return nil and delete the objects. If there are other setObject operations that haven’t been completed before objectForKey is called, the system will keep waiting.

  • (void)removeObjectForKey:(NSString*)key;

    Delete objects.

  • (void)removeAllObjects;

    Delete all objects.

  • (void)addObjects:(NSDictionary*)objects;

    Add data in batch.

  • (void)removeObjectsWithSqlLike:(NSString*)like;

    Delete data in batch. The key of data uses sqlite like statement to match.

  • (void)removeObjectsWithKeys:(NSSet*)keys;

    Delete all the data that corresponding to the keys in batch.