The cache part of bigapple's cache is used

1. Begin

Some data needs to be loaded on the network, but it cannot be loaded every time, so this time it is generally cached locally. There are many media cached to local. You can make files, or local databases. This time we are caching to memory.


2. Use object caching to cache objects.

Cache<String, Object> cache = AnCacheUtils.getObjectMemoryCache();
cache.put("xuan", "你好,我被缓存了");

The above is to cache a string. AnCacheUtils.getObjectMemoryCache() gets a default cache object. The maximum limit is 20 by default. Use the LRU caching algorithm.

Getting the cache is even easier. as follows:

Cache<String, Object> cache = AnCacheUtils.getObjectMemoryCache();
String cacheStr = (String)cache.get("xuan");
What cacheStr gets is the string that was originally stored. Of course, we also override a put method to support cache expiration. Use as follows:

Cache<String, Object> cache = AnCacheUtils.getObjectMemoryCache();
Date currentTime = new Date();
cache.put("xuan", "你好", currentTime.getTime() + 2000);// 过期2S

...中间让时间超过2S

String cacheStr = (String)cache.get("xuan");
As you can imagine, the cacheStr here is null, because the cache has expired and needs to be reloaded.


3. Bitmap cache usage. If you want to cache bitmap images, you can also use the above object cache. But it is better to use the following cache specially customized for bitmap.

Cache<String, Object> cache = AnCacheUtils.getBitmapMemoryCache();
cache.put("anan", bitmap);

...

Bitmap cacheBitmap = cache.get("anan");
This default bitmap cache size is 3M. You must have thought of it, why separate the bitmap cache from the object cache. The memory capacity of the object is not well calculated, so use it to calculate. The particularity of bitmap, if limited by the number, it is easy to appear OOM. So use his occupied memory as a limit.


4. Refresh the cache and close the cache

Cache<String, Object> cache = AnCacheUtils.getObjectMemoryCache();
cache.removeAll();

...想要关闭默认缓存可以如下

AnCacheUtils.closeObjectCache();


The bitmap cache is also used similarly. cache.removeAll() will only clear the cache, but the cache container is still there. And AnCacheUtils.closeObjectCache() will set the entire cache container to null. Generally, to refresh the cache, use cache.removeAll().


5. We can also customize the default cache limit. Take object caching as an example.

AnCacheUtils.configObjectCacheSize(30);
Cache<String, Object> cache = AnCacheUtils.getObjectMemoryCache();
It should be noted that once the cache limit is re-limited. Then the previous cache will be cleared. This operation is best set when the program is initialized, and then remains unchanged after that.


6. Summary

In fact, the usage of this cache is relatively simple. It can be regarded as a map, put it when caching, and get it when caching is needed.

The source code is available on github: https://github.com/bigapple520520/bigapple 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326087427&siteId=291194637