Poco Library--foundation Component Cache caches

Source: Internet
Author: User


Caching cache: Internal provides a variety of cache caching mechanism, and different mechanisms of management cache policy implementation;


Validargs.h:validargs valid key parameter class, template parameter implementation, _key: Key, _isvalid: Whether it is valid, also provides key to get the key, isValid whether the key is valid, invalidate makes the key value invalid; The key is valid by default;


Keyvalueargs.h:keyvalueargs key value parameter to class, template parameter implementation, _key: Key, _value: Key value, also provide key to get key, value gets key value;


EventArgs.h: Event parameter class, does not provide any valid operation implementation, may act as some parameter placeholder or some template, does not provide the sending data and so on the State;


Abstractstrategy.h:abstractstrategy the abstract base class of all cache policy classes, provides the necessary interface implementations; OnUpdate: Updates existing entries, internally by calling OnRemove and Onadd implementation updates The specific removal and addition of entry operation details are done by subclasses, similar to template methods; Onadd, OnRemove, respectively, add and remove entries; Onget the interface that is invoked when a readable event occurs, OnClear removes all elements from the cache Onisvalid query whether an entry is valid, Onreplace: Remove the redundant entries in the buffer, update the cache container, the above interface is generally passive call that is caused by the corresponding cache cached object internal trigger causes the above interface is called, the parameters in some interfaces psender the sender, Generally refers to the buffer cache object that corresponds to triggering the call;


Abstractcache.h:abstractcache all cache abstract base class, based on template parameter implementation, provide external cache policy, lock mechanism; _strategy: Cache Policy Object, _data: Data container, internal use map to save each item element corresponding key value; _ Mutex: Lock mechanism object; Add, Update, Remove, Get, Clear, IsValid, replace provide multiple event objects for different operations, constructor call initialize, implement each event object to register each event delegate call event interface Uninitialize is responsible for anti-registration; Add adds a key-value pair to the cache, internally calls Doadd (removes the current key entry and advertises the Add event, then adds the key-value pair to the _data cache container, and then calls the Doreplace implementation to update the replacement element) Update key value pair, internal call doupdate (if the key is not found to add directly to the cache, otherwise update the key value, and then call the Doreplace implementation Update replacement element); Remove removes the key-value pair for the specified entry, and internally calls Doremove (if a key-value pair is found, Remove advertises the event and removes the key-value pair from the _data cache container; The has query specifies a key-value pair, internally called Dohas (if a key-value pair is found, the IsValid advertises the event and returns the key-value pair is valid); Get gets a key value, internally called Doget (if a key-value pair is found, The Get notification event, which is returned if the key value pair is valid, or if the key value pair is returned null); clear removes all entry elements, internally calls Doclear (calls the clear event advertisement and empties the _data cache container); Size returns the number of element entries in the _data cache container, Forcereplace force updates the cache container; Getallkeys gets all the keys in the cache container;

LRU Cache, based on the least recent access policy:


LRUCache.h: Recently at least the use of cache classes, inherited from the Abstractcache class, the constructor parameter CacheSize (1024) entries, that is, to allow the maximum cache entries, and no other interface implementation, the adoption of the cache policy is lrustrategy;


Lrustrategy.h:lrustrategy Recent minimum use of the policy class, _size: Cache can hold the number of keys; _keys: Key list container; _keyindex: Key, _keys iterator to map container;


Now we can combine caching policies and caching classes to specifically analyze how to handle:
The add interface in the cache class to add a new key-value pair:
1. Internal call Doadd, and doadd internal to the key value pair whether there is a current cache, if there is a doremove call from the current cache to remove the key value pair, Doremove inside is to advertise the Remove event object to remove the specified key. This is called the OnRemove function of the Lrustrategy class, removing the _keys and _keyindex corresponding keys of the Lrustrategy class and the corresponding key-value pairs of the _data cache container in the current cache;
2. Call add to advertise the event object, that is, to call the Lrustrategy class of Onadd,onadd, adding the current new key to _keys the front end and the _keyindex insert key, key iterator map pair,
3. _data cache container to increase the current new key value pair;
4. Call Doreplace, internally calling replace to advertise the event object, which is called the Onreplace of the Lrustrategy class, to update the key-value pairs in the Lrustrategy class. Keep up-to-date key-value pairs up to CacheSize entries and return the old (end-of-list) keys to the cache container, allowing the cache container to delete the old key-value pairs, so doreplace Delme Save the key collection that will be deleted;
5. Loop calls Doremove to remove all the keys in Delme, in fact Doremove internally calls the Remove event object to remove the extra keys from the Lrustrategy class, and then remove the key-value pairs in the _data cache container for the corresponding key;

Cache the update interface in the class to update the key-value pairs:
1. Internally called DoUpdate, while the doupdate internal implementation looks for the existence of the key-value pair in the _data cache container, if it does not exist, calls the Add Advertisement event object directly and inserts the current key-value pair in _data, or, if present, calls the Update Advertisement event object ( In fact, the internal use of separate calls OnRemove, Onadd implementation of the update policy class keys);
2. Call Doreplace, implement cache entry for Update cache container and Lrustrategy class, etc.;

The Remove interface in the cache class to remove the specified key-value pair:
1. Call Doremove internally to find out if the key value pair exists in the current cache container, call Remove to advertise the event object, remove the current cache from _data, or do not process; The has interface in the cache class to query whether the specified key-value pair is valid:
2. Call Dohas internally to find out if the key value pair exists in the current cache container, and if so, call IsValid to advertise the event object, calling the Onisvalid of the Lrustrategy class to query for the presence of the key in _keyindex and return whether the key is valid. Valid returns True, otherwise false is returned;
The Get interface in the cache class to query for the specified key-value pair:
3. Call Doget internally to find out if the key value pair exists in the current cache container, and if so, call the Get Advertisement event object, that is, call the Lrustrategy class Onget,onget internal Query the key, if there is a hit, that is, adjust the key to the list header, In addition, the IsValid advertisement event object is also called in Doget, the corresponding value of the key is returned to the valid key, otherwise null value is returned, and the interface such as clear, size, Forcereplace, Getallkeys in the cache class also provides queries, empties the cache, Force Flush Cache Operations: Therefore, for the cache class is responsible for maintaining the cache key value pairs, in addition to provide external operation interface, as for the cache policy, the cache policy class is responsible for maintenance;

Expire cache, timestamp-based policy:


ExpireCache.h: Timestamp-based cache class, inherited from the Abstractcache class, the constructor parameter expire (600000), that is, the maximum time to delay 10 minutes, in addition to no other interface implementation, The cache policy adopted is expirestrategy;


Expirestrategy.h:expirestrategy Timestamp-based policy class, _expiretime: Overdue timestamp, _keys: Key, _keys iterator to the map container, _keyindex: The underlying container is std:: Multimap timestamp, key pair; because the cache class and the LRUCache cache class are similar, the different points are in the cache strategy, so the analysis of the Expirestrategy class of various processing strategies;
Onadd interface: Whenever a new cache key value pair is added, the timestamp index key pair is constructed and the _keyindex is inserted, and the constructor key, the iterator pair inserts the _keys, and the _keyindex is updated directly for the insertion failure;
OnRemove interface: Find out if there is a corresponding key in the _keys, if present, then remove the corresponding key from _keyindex and _keys;
Onget interface: When hit, do not do any processing;
OnClear interface: Direct emptying of all key-value pairs in _keyindex and _keys;
Onisvalid interface: Find out if there is a corresponding key in the _keys, if there is a time stamp, if it is past or not, set the corresponding key value validity is invalid;
Onreplace interface: Removes all _keyindex key-value pairs that have timed out directly to the Elemstoremove collection;

EXPIRELRU cache, combined with recent minimum access policy and timestamp strategy;


Expirelrucache.h:expirelrucache combines recent minimum access policy and timestamp policy, inherits from Abstractcache class, constructor function CacheSize (1024) entry, expire (600000), That is, the maximum overdue time is 10 minutes, the internal _strategy of the constructor increases the two policy objects, and there is no other interface implementation, the cache strategy is strategycollection;


Strategycollection.h:strategycollection based on recent minimum access policy and timestamp policy classes, _strategies: Policy container, save current policy set, additional interface pushback: Increase policy Object ; Popback: Remove the recently added policy object, and the following will implement the description for the base class interface;
Onadd, OnRemove, Onget, OnClear, Onisvalid, onreplace interfaces: Traversal policy containers, which call the Onadd interface implementation of each policy object in turn;

The Accessexpire cache, which is similar to the Expirestrategy strategy, differs in that the interface onget updates the timestamp;


Accessexpirecache.h:accessexpirecache can access the update timestamp policy, which inherits from the Abstractcache class, the constructor parameter expire (600000), which allows for a maximum overdue time of 10 minutes, In addition, there is no other interface implementation, the adopted cache policy is accessexpirestrategy;


Accessexpirestrategy.h:accessexpirestrategy inherits from the Expirestrategy policy class and overrides the function Onget, which updates the current project time stamp to the latest timestamp;

ACCESSEXPIRELRU cache, using a combination of recent minimum access policy and update timestamp strategy;


Accessexpirelrucache.h:accessexpirelrucache combines recent minimum access policy and update timestamp policy, inherits from Abstractcache class, constructor function CacheSize (1024) entry, expire (600000), that is, allow the maximum overdue time 10 minutes, the constructor internal _strategy added the two policy objects, in addition, there is no other interface implementation, the use of the cache policy is strategycollection;

Uniqueexpire cache, using Uniqueexpirestrategy policy for cache management;


The Uniqueexpirecache.h:uniqueexpirecache cache is similar to relying on the expire cache, except that TValue needs to provide a Getexpiration implementation interface, which is the time stamp provided by TValue;


The Uniqueexpirestrategy.h:uniqueexpirestrategy policy class, inherited from the Abstractstrategy, differs from the Expirestrategy class in the Onadd interface implementation The timestamp needs to be provided by the TValue Getexpiration implementation to obtain the corresponding timestamp of the TValue;

UNIQUEEXPIRELRU cache, using a combination of recent minimum access policy and uniqueexpirestrategy strategy;


The Uniqueexpirelrucache.h:uniqueexpirelrucache cache uses the Strategycollection policy collection to manage the cache;

Uniqueaccessexpire cache, using Uniqueaccessexpirestrategy policy for cache management;


Uniqueaccessexpirecache.h:uniqueaccessexpirecache Cache inherits from Abstractcache, which requires TValue to provide gettimeout get time-out timestamp , which is a fixed relative time-out;
The Uniqueaccessexpirestrategy.h:uniqueaccessexpirestrategy class inherits from the Abstractstrategy policy class, The Onadd interface in the interface is calculated by calculating the relative time and current implementation as the expiry timestamp of the current item, and other interface implementations are similar;

UNIQUEACCESSEXPIRELRU cache, using a combination of recent minimum access policy and uniqueaccessexpirestrategy strategy;


The Uniqueaccessexpirelrucache.h:uniqueaccessexpirelrucache class inherits from the Abstractcache, using the Strategycollection policy collection to manage the cache The cache relative to the Uniqueaccessexpire cache adds a recent minimum access policy, that is, the Onget interface fixes updates to the currently accessed cache entry timestamp;

Poco Library--foundation Component Cache caches

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.