jcs-基本概念和使用

我不是女神ヾ 2022-12-04 07:44 206阅读 0赞

前言

如果一个计算非常耗时,每次获取都重新计算,会消耗很多额外的计算资源。对于这些耗时的计算结果,可以存储在缓存中,下次需要的时候,直接访问缓存中的内容,而不是再重新计算一遍。对于易变性还比较高的数据,可以设置数据有效期,一旦超过有效期,缓存数据被移除,触发重新计算。

下面来看看apache的缓存实现:java cache system。

基本概念

元素(elements)

元素是放入缓存中的对象,可以通过键来访问。

区域(regions)

缓存是多个字典的集合,每个字典被称为region,每个region可以单独配置。比如一个region可以用来存放更新频率低的城市数据,另外一个region用来存放更新频率高的商品数据。

备用(auxiliary)

“auxiliary”是一个region可以使用的可选插件。核心辅助工具是索引磁盘缓存、TCP横向缓存和远程缓存服务器。

配置

cache.ccf

  1. # DEFAULT CACHE REGION
  2. jcs.default=DC
  3. jcs.default.cacheattributes=org.apache.commons.jcs.engine.CompositeCacheAttributes
  4. jcs.default.cacheattributes.MaxObjects=1000
  5. jcs.default.cacheattributes.MemoryCacheName=org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache
  6. jcs.default.cacheattributes.UseMemoryShrinker=false
  7. jcs.default.cacheattributes.MaxMemoryIdleTimeSeconds=3600
  8. jcs.default.cacheattributes.ShrinkerIntervalSeconds=60
  9. jcs.default.elementattributes=org.apache.commons.jcs.engine.ElementAttributes
  10. jcs.default.elementattributes.IsEternal=false
  11. jcs.default.elementattributes.MaxLife=21600
  12. jcs.default.elementattributes.IdleTime=1800
  13. jcs.default.elementattributes.IsSpool=true
  14. jcs.default.elementattributes.IsRemote=true
  15. jcs.default.elementattributes.IsLateral=true
  16. # PRE-DEFINED CACHE REGIONS
  17. #jcs.region.testCache1=DC
  18. #jcs.region.testCache1.cacheattributes=org.apache.commons.jcs.engine.Com#positeCacheAttributes
  19. #jcs.region.testCache1.cacheattributes.MaxObjects=1000
  20. #jcs.region.testCache1.cacheattributes.MemoryCacheName=org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache
  21. #jcs.region.testCache1.cacheattributes.UseMemoryShrinker=false
  22. #jcs.region.testCache1.cacheattributes.MaxMemoryIdleTimeSeconds=3600
  23. #jcs.region.testCache1.cacheattributes.ShrinkerIntervalSeconds=60
  24. #jcs.region.testCache1.cacheattributes.MaxSpoolPerRun=500
  25. #jcs.region.testCache1.elementattributes=org.apache.commons.jcs.engine.ElementAttributes
  26. #jcs.region.testCache1.elementattributes.IsEternal=false
  27. # AVAILABLE AUXILIARY CACHES
  28. jcs.auxiliary.DC=org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory
  29. jcs.auxiliary.DC.attributes=org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes
  30. jcs.auxiliary.DC.attributes.DiskPath=${ user.dir}/jcs_swap
  31. jcs.auxiliary.DC.attributes.MaxPurgatorySize=10000000
  32. jcs.auxiliary.DC.attributes.MaxKeySize=1000000
  33. jcs.auxiliary.DC.attributes.OptimizeAtRemoveCount=300000
  34. jcs.auxiliary.DC.attributes.ShutdownSpoolTimeLimit=60

样板代码

  1. package xxl.mathematica.cache;
  2. import org.apache.commons.jcs.JCS;
  3. import org.apache.commons.jcs.access.behavior.ICacheAccess;
  4. import org.apache.commons.jcs.engine.behavior.ICacheElement;
  5. import java.util.Map;
  6. /** * @author zhangliangbo * @since 2020/9/6 **/
  7. public class Cache {
  8. private static final ICacheAccess<String, Object> I_CACHE_ACCESS = JCS.getInstance("default");
  9. /** * 获取值 * * @param key 键 * @return 值 */
  10. public static Object get(String key) {
  11. return I_CACHE_ACCESS.get(key);
  12. }
  13. /** * 获取最大空闲时间(超过最大空闲时间,值会被移除) * 单位:s * 默认值参考cache.ccf * * @param key 键 * @return 值 */
  14. public static long idleTime(String key) {
  15. return I_CACHE_ACCESS.getCacheElement(key).getElementAttributes().getIdleTime();
  16. }
  17. /** * 获取最大生存时间(超过最大生存空间,值会被移除,即使在最大空闲时间内被刷新) * 单位:s * 默认值参考cache.ccf * * @param key 键 * @return 值 */
  18. public static long maxLife(String key) {
  19. return I_CACHE_ACCESS.getCacheElement(key).getElementAttributes().getMaxLife();
  20. }
  21. /** * 匹配正则表达式的键值对 * * @return 键值对 */
  22. public static Map<String, Object> match(String pattern) {
  23. return I_CACHE_ACCESS.getMatching(pattern);
  24. }
  25. /** * 放入值 * * @param key 键 * @param value 值 */
  26. public static void put(String key, Object value) {
  27. I_CACHE_ACCESS.put(key, value);
  28. }
  29. /** * 放入值,带有效期 * * @param key 键 * @param value 值 * @param idleSeconds 有效期 */
  30. public static void put(String key, Object value, long idleSeconds) {
  31. I_CACHE_ACCESS.put(key, value);
  32. ICacheElement<String, Object> element = I_CACHE_ACCESS.getCacheElement(key);
  33. element.getElementAttributes().setIdleTime(idleSeconds);
  34. }
  35. /** * 键不存在时放置 * * @param key 键 * @param value 值 */
  36. public static void putNx(String key, Object value) {
  37. I_CACHE_ACCESS.putSafe(key, value);
  38. }
  39. /** * 清空缓存 */
  40. public static void clear() {
  41. I_CACHE_ACCESS.clear();
  42. }
  43. /** * 移除键 * * @param key 键 */
  44. public static void remove(String key) {
  45. I_CACHE_ACCESS.remove(key);
  46. }
  47. /** * 获取缓存的大小 * * @return 缓存大小 */
  48. public static int size() {
  49. return I_CACHE_ACCESS.getMatching(".*").size();
  50. }
  51. /** * 获取缓存的容量 * * @return 缓存容量 */
  52. public static int capacity() {
  53. return I_CACHE_ACCESS.getCacheAttributes().getMaxObjects();
  54. }
  55. }

发表评论

表情:
评论列表 (有 0 条评论,206人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Git 的基本概念使用方式

    一、Git基本概念 Git是一种分布式版本控制系统,它可以追踪文件的变化,并允许多人同时对同一文件进行更新和合并。以下是Git的基本概念和使用方式: 1. 仓库(Re

    相关 jcs-基本概念使用

    前言 如果一个计算非常耗时,每次获取都重新计算,会消耗很多额外的计算资源。对于这些耗时的计算结果,可以存储在缓存中,下次需要的时候,直接访问缓存中的内容,而不是再重新计算