【问题标题】:Usage of springcache plugin in grailsgrails中springcache插件的使用
【发布时间】:2012-07-06 08:32:45
【问题描述】:

我一直在尝试将 SpringCache 添加到我的 Spring 托管服务之一。我使用 STS 作为我的开发 IDE。

这是我所做的:

  1. 安装springcache 1.3.1插件

  2. 向 Config.groovy 添加缓存配置:

    grails.spring.disable.aspectj.autoweaving = true
    
    springcache {
      enabled = true
      defaults {
          eternal = false
          diskPersistent = false
      }
      caches {
          tripCache {
              memoryStoreEvictionPolicy = "LRU"
          }
      }
    }
    
  3. 在服务中我的测试方法上方添加@Cacheable注解如下:

      int counter = 0
    
      @Cacheable("testCache")
      String testInc(String x) {
        return (++counter).toString()
      }
    
  4. 现在,我从控制器调用这个方法如下:

      def testDashboard() {
        [data:testService.testInc("1")]
      }
    

testInc() 总是返回一个新值,即连续调用返回 1、2、3 等,这让我认为缓存不起作用。我错过了什么并且没有正确打开缓存吗?

我试图查看缓存是否正常工作,我挂钩了 ehcache-2.4.6 的源代码并在 CachingAspect 的 invokeCachedMethod 中设置断点,但是当我运行我的应用程序时,上述方法似乎没有被执行。

我还尝试在缓存中查看“1”的值,看起来它为空,即键“1”没有存储在缓存中。我在控制器测试方法中添加了以下两行:

      Ehcache c = springcacheService.getOrCreateCache("testCache")
      String x = c.get("1")

请帮忙!

【问题讨论】:

    标签: caching grails service aspectj ehcache


    【解决方案1】:

    基本上,我被这里的 springcache 文档误导了: http://gpc.github.com/grails-springcache/docs/guide/3.%20Caching%20Service%20Methods.html

    Config.groovy 应该有 grails.spring.disable.aspectj.autoweaving = false 而不是 true。

    希望这对那些陷入同一问题的人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 2015-04-22
      • 2014-02-25
      相关资源
      最近更新 更多