redis在項目中的使用

緩存的使用就是爲了提升效率,避免重複的IO操做浪費效率。redis

查詢時使用,如selectById value:緩存區名稱,key:在緩存區內對應的鍵, 表示查詢緩存區「user」中key爲參數id的緩存,若是沒有則查詢數據庫,並把數據放入緩存中(注意這裏緩存的數據是指方法執行完成返回的結果),之後直接從緩存取數據。 @Cacheable(key = "#id", value = "user")數據庫

查詢時使用,如getAll value:緩存區名稱,key:沒有指定採用默認生成策略(本項目使用:cn.my.base.RedisCacheConfig) @Cacheable(value = "users")緩存

插入數據使用:@CachePut註解的方法必定會執行,無論有沒有緩存,方法的返回值放入緩存中 @CachePut(value = "user", key = "#user.id").net

刪除、更新時使用:beforeInvocation=true表示無論方法執行是否成功,在方法執行以前刪除緩存 這裏注意緩存必定要刪除乾淨,不只要刪除「user」緩存區,還要刪除「users」緩存區 @CacheEvict(key = "#user.id", value = "user", beforeInvocation = true) @CacheEvict(value="users",allEntries=true,beforeInvocation=true)blog

像上邊這種一下執行兩條及以上緩存操做的,要用組合緩存操做,即改成 @Caching(   evict={    @CacheEvict(key = "#user.id", value = "user", beforeInvocation = true),    @CacheEvict(value="users",allEntries=true,beforeInvocation=true)   } )get

更多瞭解redisit

http://xp9802.iteye.com/blog/2121997
http://blog.csdn.net/defonds/article/details/48716161io

相關文章
相關標籤/搜索