有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JCS缓存删除功能不删除特定元素

JCS缓存-删除功能不删除特定元素

应用程序的jcs缓存配置如下所示,我看到当我们使用fulshAll()方法时,它会删除整个缓存,但当我们使用remove(key)时,它不会删除该对象。有人能推荐一下吗

public static void init( java.util.Properties properties ) {        
    java.util.Properties cacheProperties = new java.util.Properties();
    java.util.regex.Pattern cachePattern = 
    java.util.regex.Pattern.compile("^jcs.*");

    for (String key:properties.stringPropertyNames()) {
        Matcher cacheMatcher = cachePattern.matcher(key);

        if ( cacheMatcher.find() ) {
            cacheProperties.setProperty(key,properties.getProperty(key));
        }
    }

    CompositeCacheManager ccm = 
    CompositeCacheManager.getUnconfiguredInstance();
    ccm.configure(cacheProperties);
    miscCacheAdministrator = JCS.getInstance("miscCache");  
    metaDataCacheAdministrator = JCS.getInstance("metaDataCache");
    resultCacheAdministrator = JCS.getInstance("resultCache");
}

我把一个元素放在缓存中,并在这里移除它进行演示

public static void ExampleCache(String key){
    resultCacheAdministrator.put(key, "Temp Cache");        
    resultCacheAdministrator.remove(key);
    logger.debug(" Flushing a Particular Cache "+key);  
}

调用PUT时,我看到该对象存储了1kb,我立即使用相同的密钥调用remove进行测试,我看到该对象仍然存在并且没有从缓存中删除,我希望1kb为0,请让我知道我在这里做错了什么,为什么缓存对象没有从文件缓存中删除

enter image description here

属性文件

# cache settings
jcs.region.resultCache=DC
jcs.region.resultCache.cacheattributes.MaxObjects=0
jcs.region.resultCache.elementattributes.IsEternal=false
jcs.region.resultCache.elementattributes.MaxLife=14400
jcs.region.resultCache.elementattributes.IsSpool=true
jcs.region.resultCache.cacheattributes=org.apache.commons.jcs.engine.
CompositeCacheAttributes

 # Disk Cache Event Queue Pool
 thread_pool.disk_cache_event_queue.useBoundary=false
 thread_pool.disk_cache_event_queue.maximumPoolSize=3
 thread_pool.disk_cache_event_queue.minimumPoolSize=1
 thread_pool.disk_cache_event_queue.keepAliveTime=3500
 thread_pool.disk_cache_event_queue.startUpSize=1

共 (1) 个答案

  1. # 1 楼答案

    从提供的信息来看,没有事实证明磁盘文件会缩小

    简短回答如下:

    从提供的配置中,您可能希望通过设置

    jcs.region.resultCache.cacheattributes.OptimizeAtRemoveCount=1
    

    顺便说一句,查看代码,您可能会发现,为了在运行时启用优化,所述属性的值必须为> 0

    关于更详细的解释:

    当谈论磁盘文件时,我们谈论的是AbstractDiskCache类的实现。目前有三个子类:
    BlockDiskCacheIndexedDiskCacheJDBCDiskCache

    • BlockDiskCache不显示任何管理结果文件大小的机制。受保护的freeBlocks方法只是记录为“将这些块添加到清空块列表

    • JDBCDiskCache显然是将管理存储大小的任务委托给底层数据库系统

    • 这就剩下了IndexedDiskCache。作为默认实现,它可能正在您的案例中使用
      这个类展示了指示所需功能的optimizeFile()方法。在优化过程中,数据文件被压缩,结果文件的大小减小

    如果发生以下两个事件之一,则会触发此类优化

    • 关闭时关闭
      关于OptimizeOnShutdown财产状态的文件:

      By default the Indexed Disk Cache will optimize on shutdown if the free data size is greater than 0. If you want to prevent this behavior, you can set this parameter to false.

      默认情况下,该值为true

    • 移除次数
      关于OptimizeAtRemoveCount财产状态的文件:

      At how many removes should the cache try to defragment the data file. Since we recycle empty spots, defragmentation is usually not needed. To prevent the cache from defragmenting the data file, you can set this to -1. This is the default value.

    但是,您的属性不显示要设置的OptimizeAtRemoveCount。因此,任何释放的项都将添加到空闲列表中以供重用,但磁盘文件大小不会减小

    从磁盘缓存的分布式实现来看,只有IndexedDiskCache提供了减少磁盘文件大小的功能。您需要正确配置优化功能,以充分利用它。 当前任何其他操作都需要恢复到编写自己的派生类

    另请注意:
    磁盘文件优化与炼狱逻辑完全不同。后者涉及从缓存本身删除值。这样的移除可能会导致磁盘大小减小