Implement object caching Framework Iii. Zsycachequeue

Source: Internet
Author: User

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:

    1. Pass in a cache entry "object, key, time-out interval"
    2. Wrapping the object and time-out interval as a Zsycacheobject object
    3. Use "Zsycacheholder object" to save this "Zsycacheobejct object" by "key"--"Zsycacheobejct object is saved with a dictionary, and there is no order"
    4. Detects if the current keys queue is full and if it is full
    5. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.