Use redis to cache the method of querying data in the ssm project

  • 2020-06-07 05:31:06
  • OfStack

In projects, the persistence layer of the background program is often required to query the database for data, and then pass the data to the service layer, the control layer, and finally to the view layer. If the data access is slow, it will affect the operation of the program.

To speed up your program, you can put data into a cache, including a data cache and a page cache.

The so-called cache, is the program or system often to call the object in memory, once its use can be called quickly, no need to create a new duplicate instance. This can reduce system overhead and improve system efficiency.

Page cache is mainly oscache, which can cache the whole page or specify a part of the page, and specify its expiration time, so that the data accessed during this time period are all of one kind.

There are many data caches, such as ehcache, redis, memcached, etc.

This paper mainly USES Java code to carry out redis cache, that is, to obtain data from redis cache in service layer when querying. If it does not exist, it will be obtained from the database through the dao layer, and finally the queried data will be cached in redis. If present, it is read directly from the redis cache and handed to the controller layer.

xml configuration:


<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
 <property name="maxTotal" value="${redis.maxTotal}"></property> 
 <property name="maxIdle" value="${redis.maxIdle}"></property> 
 <property name="testOnBorrow" value="${redis.testOnBorrow}"></property> 
 <property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property> 
</bean> 
<!-- jedis Client stand-alone version  --> 
<bean id="jedisPool" class="redis.clients.jedis.JedisPool"> 
 <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg> 
 <constructor-arg name="host" value="${redis.hostName}"></constructor-arg> 
 <constructor-arg name="port" value="${redis.port}"></constructor-arg> 
</bean> 
<!-- jedis Cluster version configuration  --> 
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> 
 <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg> 
 <constructor-arg name="nodes"> 
 <set> 
  <bean class="redis.clients.jedis.HostAndPort"> 
  <constructor-arg name="host" value="127.0.0.1"></constructor-arg> 
  <constructor-arg name="port" value="7001"></constructor-arg> 
  </bean> 
  <bean class="redis.clients.jedis.HostAndPort"> 
  <constructor-arg name="host" value="127.0.0.1"></constructor-arg> 
  <constructor-arg name="port" value="7002"></constructor-arg> 
  </bean> 
  <bean class="redis.clients.jedis.HostAndPort"> 
  <constructor-arg name="host" value="127.0.0.1"></constructor-arg> 
  <constructor-arg name="port" value="7003"></constructor-arg> 
  </bean> 
  <bean class="redis.clients.jedis.HostAndPort"> 
  <constructor-arg name="host" value="127.0.0.1"></constructor-arg> 
  <constructor-arg name="port" value="7004"></constructor-arg> 
  </bean> 
  <bean class="redis.clients.jedis.HostAndPort"> 
  <constructor-arg name="host" value="127.0.0.1"></constructor-arg> 
  <constructor-arg name="port" value="7005"></constructor-arg> 
  </bean> 
 </set> 
 </constructor-arg> 
</bean> 
[java] view plain copy
java call  
//service Layer query time code  
@Autowired 
private JedisClient jedisClient; 
@Override 
public List<User> list() { 
 // TODO Auto-generated method stub 
 try { 
 String json = jedisClient.hget("user", "list"); 
 if (StringUtils.isNotBlank(json)) { 
  List<User> users = JsonUtils.jsonToList(json, User.class); 
  return users; 
 } 
 } catch (Exception e) { 
 e.printStackTrace(); 
 } 
 // Query the database using the persistence layer  
 List<User> list = userMapper.list(); 
 try { 
 jedisClient.hset("user", "list", JsonUtils.objectToJson(list)); 
 } catch (Exception e) { 
 e.printStackTrace(); 
 } 
 return list; 
} 

conclusion


Related articles: