Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use @ CachePut to update the database and cache

Shulou Source: shulou.com Published: 2022-06-02 05:03:27 09月30日 Update

Editor to share with you how to use @ CachePut to update the database and update cache, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

About updating the cache, pay attention to two points: 1, @ Cacheable's key

To be consistent with @ CachePut's key

For example:

@ Cacheable (key = "'userCache'") / / cache, public Uuser findByEmail (String email) {System.err.println ("execute here, indicating that the data cannot be read in the cache, read the database directly."); return redisMapper.findByEmail (email) } @ CachePut (key = "'userCache'") / / userCache is to be enclosed in' 'single quotes, indicating that this is a string public Uuser updateSelf (String nickname, String email) {System.err.println ("execute here, update the database, update the cache."); uuserMapper.updateSelf (nickname, email); Uuser uuser = redisMapper.findByEmail (email); return values of return uuser;} 2 and @ CachePut

To be the same as the return value of @ Cacheable

If @ Cacheable returns an object, @ CachePut returns an object, otherwise it will report a type conversion exception, such as Uuser.

Cached CachePut conflict Cacheable

When CachePut and Cacheable are put together, Cacheable has the same effect as CachePut, checking the database every time, even though there is a cache.

/ * * @ param id * @ return * / @ Caching (put = {@ CachePut (key = "T (cn.a.b.constant.RedisKey). OPEN_MEDIUM_INFO + # result.mediumBankCard", unless= "# result.mediumBankCard==null or # result.status! ='2'"), @ CachePut (key = "T (a.b.c.constant.RedisKey). ACCOUNT_CODE + # result.accountCode", unless= "! '3'} .coach (# result.mediumStatus)), @ CachePut (key= "T (a.b.c.constant.RedisKey). CERT_NO+ # result.certNo", unless= "# result.certNo==null or! {' 0# result.status))}, cacheable = {@ Cacheable (key=" T (a.b.c.constant.RedisKey). ID + # id ") public XXXInfo selectByPrimaryKey (Long id) {return mapper.selectByPrimaryKey (id) } can be decomposed into two Service.java/** @ param id * @ return * / @ Caching (put = {@ CachePut (key = "T (cn.a.b.constant.RedisKey). OPEN_MEDIUM_INFO + # result.mediumBankCard", unless= "# result.mediumBankCard==null or # result.status! ='2'"), @ CachePut (key = "T (a.b.c.constant.RedisKey). ACCOUNT_CODE + # result.accountCode", unless= "! {'0' (# result.mediumStatus), @ CachePut (key = "T (a.b.c.constant.RedisKey). CERT_NO+ # result.certNo", unless= "# result.certNo==null or! {# result.status))}) public XXXInfo selectByPrimaryKey (Long id) {return mapper.selectByPrimaryKey (id)) } Mapper.java {@ Cacheable (key= "T (a.b.c.constant.RedisKey). ID + # p0") XXXXInfo selectByPrimaryKey (Long id);}

Mybatis interface class parameters. Invalid # parameter name. Can only use # p0, # p1

The above is all the contents of the article "how to use @ CachePut to update the database and update the cache". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Cache update data database articles content parameters objects consistent not much two code most such as above characters strings quotes interfaces effects more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Technology Shulou Information Redmi NVidia