Zsycachequeue is used as a caching method for implementing queue storage objects.
- Zsycache relies on a Zsycacheholder object
- Zsycachequeue object persistence using the Zsycacheholder object held by the Zsycache object when the object is created
- Action 1: Queue an Object
- Action 2: Bring the object out of the team
- Zsycachequeue object. Keysqueue: This array holds the key corresponding to the object that is enqueued in sequence
- Zsycachequeue object. Offset: This property is the total cheap amount
Specific logical steps:
- Pass in a cache entry "object, key, time-out interval"
- Wrapping the object and time-out interval as a Zsycacheobject object
- Use "Zsycacheholder object" to save this "Zsycacheobejct object" by "key"--"Zsycacheobejct object is saved with a dictionary, and there is no order"
- Detects if the current keys queue is full and if it is full
- Put the newest key into the Keysqueue tail
Summarize:
1. Keysqueue saves the key of the queued object in order of queue, and the object is sealed into Zsycacheobect, which is saved by the Zsycacheholder object using a dictionary.
2. Find the original object, first go to Keysqueue to find the key, and then through the Zsycacheholder object using key to "Dictionary object" query to the Zsycacheobject object, and then through the Zsycacheobject method to get the original cache object.
Start pasting code:
//. h#import<Foundation/Foundation.h>@classZsycacheholder;@interfaceZsycachequeue:nsobject@property (nonatomic, strong) NSString*name; @property (nonatomic, assign) Nsinteger size; @property (nonatomic, Strong,ReadOnly) Nsmutablearray *keysqueue; @property (nonatomic, copy)void(^onpop) (IDpopedobject); @property (nonatomic, copy)void(^onexpirate) (IDpopedobject);-(Instancetype) Initwithholder: (Zsycacheholder *) holder;//Key Queue- (void) Zsypushobj: (ID) obj;//object out of the team- (ID) Zsypopobj;@end
//init, use the Zsycacheholder of the Zsycache object to complete subsequent persisted object operations -(instancetype) Initwithholder: (Zsycacheholder *) holder { = [Super init]; if (self) { = holder; = zsycache_default_queue_size; 0 ; = [Nsmutablearray array]; } return Self ;}
//queue the objects that will be cached- (void) Zsypushobj: (ID) obj {//offset + +self.offset++; //detects timeout cache object, once timeout, delete corresponding key from cache queue, local NSData file[self _cleanexpirateobjects]; //gets the corresponding unique key for the cached object in the queueNSString *key = [NSString stringWithFormat:@"queue_%@_%ld", Self.name, Self.offset]; //use a unique key as the identity of the object into the queue[_holder zsysetobject:obj Forkey:key Duration:0]; //if the queue is full at this point, the queue header object is out of the queue if(_keysqueue.count >=_size) { //Locate key in the key queue headerNSString *popkey =_keysqueue.firstobject; //find the key corresponding to the ZsycacheobjectZsycacheobject *cachedobject =[_holder Zsygetobjectforkey:key]; //executes the callback block when the object is out of the queue if(_onpop) {_onpop ([cachedobject cacheobjectvalue]); } //Delete the local cache object file using the Zsycacheholder object[_holder Zsyremoveobjectforkey:popkey];//Local removal//removes the key from the current queue header from the key array[_keysqueue Removeobject:popkey];//The memory key array is removed } //Queue key for current cache object[_keysqueue Addobject:key];}
Implement object caching Framework Iii. Zsycachequeue