Several implementations of spring method level cache

Scheme implementation

1. Spring and ehcache integration

It mainly obtains ehcache as the object of ehcache operation.

spring. Inject ehcachemanager and ehcache objects into XML. Ehcachemanager needs to load ehcache XML configuration information, create ehcache Configure caches with different policies in XML.

2. Spring and built-in cache support

3. Spring and redis integration

It mainly obtains redistemplate as the object of redis operation.

redis. Properties configuration information

#Host writes the redis server address

redis. ip=127.0. zero point one

#Port

redis. port=6379

#Passord

#redis. password=123456

#Connection timeout 30000

redis. timeout=30

#Maximum number of objects allocated

redis. pool. maxActive=100

#The maximum number of objects that can maintain the idel state

redis. pool. maxIdle=30

#The maximum waiting time when no object is returned in the pool

redis. pool. maxWait=1000

#Whether validity check is performed when the border object method is called

redis. pool. testOnBorrow=true

#Whether to check the validity when calling the return object method

redis. pool. testOnReturn=true

Spring injects jedispool, redisconnfactory and redistemplate objects

4. Spring cache annotation interpretation

There are three cache annotations:

@Cacheable @CacheEvict @CachePut

one

@Cacheable (value = "accountcache"), this annotation means that when this method is called, it will query from a cache called accountcache. If not, the actual method will be executed and the execution result will be stored in the cache. Otherwise, the object in the cache will be returned. Here, the key in the cache is the parameter username and the value is the account object. "Accountcache" cache is in spring * Name defined in XML.

example:

@Cacheable (value = "accountcache") / / a cache named accountcache is used

public Account getAccountByName(String userName) {

//The internal implementation of the method does not consider the cache logic and directly implements the business

System. out. println("real query account."+ userName);

return getFromDB(userName);

}

Condition: used to judge the condition. If the condition is met, it will be cached

Example 2:

@Cacheable (value = "accountcache", condition = "#username. Length() < = 4") / / the cache name is accountcache

public Account getAccountByName(String userName) {

//The internal implementation of the method does not consider the cache logic and directly implements the business

return getFromDB(userName);

}

two

@Cacheevict annotation to mark the method to empty the cache. When this method is called, the cache will be emptied. Note that one of @ cacheevict (value = "accountcache", key = "#account. Getname()"), in which the key is used to specify the cached key. Here, because we use the name field of the account object when saving, we also need to obtain the value of name from the parameter account object as the key. The previous # sign indicates that this is a spiel expression, This expression can traverse the parameter object of the method

Example 3:

@Cacheevict (value = "accountcache", key = "#account. Getname()") / / clear the accountcache cache

public void updateAccount(Account account) {

updateDB(account);

}

@Cacheevict (value = "accountcache", allentries = true) / / clear the accountcache cache

public void reload() {

reloadAll()

}

@Cacheable (value = "accountcache", condition = "#username. Length() < = 4") / / the cache name is accountcache

public Account getAccountByName(String userName) {

//The internal implementation of the method does not consider the cache logic and directly implements the business

return getFromDB(userName);

}

three

@Cacheput annotation, which can ensure that the method is executed, and the return value of the method is also recorded in the cache to realize the synchronous update of the cache and the database.

@Cacheput (value = "accountcache", key = "#account. Getname()") / / update the accountcache cache

public Account updateAccount(Account account) {

return updateDB(account);

}

Appendix:

@Introduction to cacheable, @ cacheput, @ cacheevict notes

From the above example, we can see that spring cache mainly uses two annotation tags, @ cacheable, @ cacheput and @ cacheevict. Let's summarize their functions and configuration methods.

Table 1@ Function and configuration method of cacheable

@Cacheable is mainly used for method configuration. It can cache the results according to the request parameters of the method

Table 2@ Cacheput function and configuration method

@Cacheput is mainly used for method configuration. It can cache the results according to the request parameters of the method. Unlike @ cacheable, it will trigger the call of the real method every time

Table 3@ Cacheevict function and configuration method

@Cachevict is mainly used for method configuration and can empty the cache according to certain conditions

The above multiple implementations of spring method level cache are all the contents shared by Xiaobian. I hope to give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>