春天 - 使用谷歌番石榴缓存

时间:2015-08-19 10:32:57

标签: java spring caching google-guava-cache

我正在尝试在我的春季应用中使用google guava缓存,但结果永远不会缓存。

这是我的步骤:

在conf文件中:

 UPDATE dbo.companyinfo SET companyinfo.companyname=case when companyinfo.companyname='' or companyinfo.companyname=null then RESULT.companyname else companyinfo.companyname end ,
 companyinfo.website= case when companyinfo.website='' OR companyinfo.website IS NULL then RESULT.website else companyinfo.website end ,
 companyinfo.contactperson= case when companyinfo.contactperson='' OR companyinfo.contactperson IS NULL then RESULT.contactperson else companyinfo.contactperson end,companyinfo.country = case when companyinfo.country=1 OR companyinfo.country IS NULL then RESULT.country else companyinfo.country end,
 companyinfo.telphone=case when companyinfo.telphone='' OR companyinfo.telphone IS NULL then RESULT.telphone else companyinfo.telphone end,companyinfo.mobile= case when companyinfo.mobile='' OR companyinfo.mobile IS NULL then RESULT.mobile else companyinfo.mobile end ,
 companyinfo.fax= case when companyinfo.fax='' OR companyinfo.fax IS NULL then RESULT.fax else companyinfo.fax end, companyinfo.region= case when companyinfo.region=2 OR companyinfo.region IS NULL then RESULT.region else companyinfo.region end,companyinfo.urlorcatalog=RESULT.urlorcatalog,companyinfo.address= case when companyinfo.address='' OR companyinfo.address IS NULL then RESULT.address else companyinfo.address end,
 companyinfo.lastupdatedby=RESULT.lastupdatedby
FROM

(   
select TEMP1.companyname,TEMP1.website,TEMP1.contactperson,TEMP1.country, TEMP1.telphone , TEMP1.mobile, TEMP1.fax,TEMP1.region, TEMP1.urlorcatalog,TEMP1.address,TEMP1.lastupdatedby, TEMP1.DataID
from

        (   
            SELECT tmp.companyname,tmp.website,tmp.contactperson,tmp.country,tmp.telphone,tmp.mobile,tmp.fax, tmp.region,tmp.urlorcatalog,tmp.address,tmp.lastupdatedby,Email.DataID,ROW_NUMBER() OVER (PARTITION BY tmp.email ORDER BY tmp.email ) AS 'RowNumber'
            FROM #TempTable tmp 
            LEFT OUTER JOIN emailinfo Email ON tmp.email =Email.email                                                   
            WHERE 
              tmp.email !=''        
            AND 
            EXISTS (SELECT  emailinfo.email FROM dbo.emailinfo WHERE email=tmp.email)
        )AS TEMP1

    LEFT OUTER JOIN dbo.companyinfo COMPANY ON TEMP1.DataID =COMPANY.dataId     

    WHERE

    TEMP1.RowNumber =1       

) AS RESULT

WHERE companyinfo.dataId =RESULT.DataID 

在课堂上,我想使用缓存:

@EnableCaching
@Configuration
public class myConfiguration {
        @Bean(name = "CacheManager")
        public CacheManager cacheManager() {
            return new GuavaCacheManager("MyCache");
        }
}

然后当我打电话时:

public class MyClass extends MyBaseClass {
    @Cacheable(value = "MyCache")
    public Integer get(String key) {
        System.out.println("cache not working");
        return 1;
    }
}

每次进入功能而不使用缓存: 控制台:

MyClass m = new MyClass();
m.get("testKey");
m.get("testKey");
m.get("testKey");

有人知道我错过了什么或者我该如何调试?

1 个答案:

答案 0 :(得分:2)

你不应该自己管理一个春豆。让春天来管理它。

@EnableCaching
@Configuration
public class myConfiguration {
        @Bean(name = "CacheManager")
        public CacheManager cacheManager() {
            return new GuavaCacheManager("MyCache");
        }

        @Bean
        public MyClass myClass(){
            return new MyClass();
        }
}

之后你应该以管理的方式使用MyClass。

public static void main(String[] args) throws Exception {
    final ApplicationContext applicationContext = new AnnotationConfigApplicationContext(myConfiguration.class);
    final MyClass myclass = applicationContext.getBean("myClass");
    myclass.get("testKey");
    myclass.get("testKey");
    myclass.get("testKey");
}