In openfire, cache is used to store frequently accessed data, such as usercache in usermanager and rostercache in rostermanager.
The implementation mechanism of cache in openfire is as follows:
1. cache interface:
Interface cache <K, V> extends java. util. Map <K, V>
Provides basic cache interface methods.
2. cacheable interface:
Interface cacheable extends java. Io. serializable
The serialization interface is inherited. If custom data needs to be stored in the cache, You need to implement the getcachesize () method in this interface. Otherwise, the following error will be reported in cache. put:
<Span style = "color: # ff0000;"> JAVA. io. notserializableexception: <br/> at java. io. objectoutputstream. writeobject0 (unknown source) <br/> at java. io. objectoutputstream. writeobject (unknown source) <br/> at Org. jivesoftware. util. cache. defaultcache. calculatesize (defaultcache. java: 583) <br/> at Org. jivesoftware. util. cache. defaultcache. put (defaultcache. java: 141) <br/> at Org. jivesoftware. util. cache. cachewrapper. put (cachewrapper. java: 129) </span>
3. cachesizes:
A class that provides the computing object size. The sizeofmap (), sizeofcollection (), and sizeofstring () methods are easy to use and can be called directly when the getcachesize () method is implemented.
4. defaultcache:
An Implementation of the cache interface in openfire, which maintains two linked lists: lastaccessedlist (for sequential access management) and agelist (for lifecycle management ).
The following methods are provided:
Put () stores a specified key-value pair. Calculate the data size. Data exceeding the maximum size of 90% set by the cache will be directly lost. The added data will be added to the two linked lists, and the cache will be sorted once. <Br/> get () searches for a data item based on the key. The system first clears the expired data, updates the hit and miss times, and moves the data from the lastaccessedlist to the head of the linked list. <Br/> remove () removes a key-value pair from the cache. Update the cache size and remove the corresponding data from the linked list. <Br/> clear () clears the entire cache. Including the linked list, the size of each statistical value. <Br/> size (): obtain the number of data in the cache. The expired data is cleared first. <Br/> isempty () determines whether it is null. The expired data is cleared first. <Br/> values () obtains all data and returns a collection. The expired data is cleared first. <Br/> containskey () determines whether the specified key is included. The expired data is cleared first. <Br/> putall () stores all data in a map to the cache. <Br/> containsvalue () determines whether the specified value exists. The expired data is cleared first. <Br/> whether containsnullvalue () contains null values. <Br/> entryset () returns a set containing all data. <Br/> keyset () returns a set containing all the keys. <Br/> getname/setname () is used to obtain the name. <Br/> getcachehits () indicates the number of times it is hit. <Br/> getcachemisses () indicates the number of Miss requests. <Br/> getcachesize () obtains the current cache size. <Br/> getmaxcachesize/setmaxcachesize () sets the maximum cache space. <Br/> getmaxlifetime/setmaxlifetime () sets the maximum lifecycle for obtaining cache data. <Br/> calculatesize () is used to calculate the size of the specified data. <Br/> Note: all the data to be stored in the cache must be of the cacheable or basic data type. If not, serialization is required to measure the data size. </P> <p> deleteexpiredentries () clears expired data. Judge nodes in the agelist linked list. <Br/> cullcache () clears some data when the cache is too full to free up space. </P> <p>
4. cachefactorystrategy interface and defaultlocalcachestrategy class:
The cachefactorystrategy interface mainly defines the Cache Management Method in cluster mode.
Defaultlocalcachestrategy is the Cache Management Method Used in openfire and does not implement the cluster function.
For the most important method, see createcache ().
5. cachefactory factory class:
The factory mode in Java design mode is used here.
Various caches are used in openfire and even our own plug-ins. cachefactory provides a unified platform for creating and using caches.
Here we will focus on three members:
Map <string, cache> caches = new concurrenthashmap <string, cache> (); </P> <p> Map <string, string> cachenames = new hashmap <string, string> (); </P> <p> Map <string, long> cacheprops = new hashmap <string, long> ();The first one is used to store all created caches.
The second is used to store all created cache names.
The third property used to store Cache
Most of the methods in the entire factory operate on these three members. The cluster is not implemented in strategy.
The createcache method calls createcache () of strategy to create a cache.
6. Use of cache:
If we need to use the cache to cache some data, we can use the openfire cache mechanism in the static {} of cachefactory {}CodeAdd our own cache. When using createcache () as needed, you must note that the synchronization and mutex of threads must be considered for cache operations.
PS: using this cache mechanism in Java programming is also a good choice. You can use it everywhere with slight modifications to the code.