Preface
Students who have worked in large software systems know that as the system data becomes larger and more complex, the problem that comes with the worse system performance, especially the performance loss caused by frequent database operation is more serious. Many bigwigs have proposed many solutions and developed many frameworks to optimize the performance losses caused by frequent operating databases. Among them, the two particularly prominent cache servers are Memcached and Redis. Today, we will not talk about Memcached and Redis itself. Here we will mainly introduce the relevant content of the integration of Spring and Redis. I won’t say much below, let’s take a look at the detailed introduction together.
The method is as follows
The first step is to add the pom code of redis to the project:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.0</version> </dependency>
The second step is to load the redis configuration file in spring: applicationContext-redis.xml, the content is as follows
<bean id="poolConfig"> <property name="maxTotal" value="${redis.maxTotal}" /></bean> <bean> <constructor-arg index="0" ref="poolConfig" /> <constructor-arg index="1"> <list> <bean> <constructor-arg index="0" value="${redis.node1.host}" /> <constructor-arg index="1" value="${redis.node1.port}" /> </bean> </list> </constructor-arg> </bean></beans>The third step is to write the property file connecting to the redis server: redis.properties
redis.maxTotal=100redis.node1.host=127.0.0.1redis.node1.port=6379
Step 4: Write the relevant operation method class, Function class and RedisService class of redis:
Funcrion class:
package xx.service;/** * In order to extract the same operation code* @author yeying *<p>Description:</p> *<p>Company:</p> * @date: December 5, 2017 at 9:02:44 pm */public interface Function<T,E> { public T callback(E e);}RedisService class:
package com.taotao.common.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import redis.clients.jedis.ShardedJedis;import redis.clients.jedis.ShardedJedisPool;/** * Related operations of redis* @author yeying *<p>Description:</p> *<p>Company:</p> * @date: December 3, 2017 at 2:11:47 pm */@Servicepublic class RedisService { @Autowired(required=false) // Need to inject private ShardedJedisPool shardedJedisPool; private <T> T execute(Function<T, ShardedJedis> fun){ ShardedJedis shardedJedis = null; try { // Get the jedis sharded object from the connection pool shardedJedis = shardedJedisPool.getResource(); // Get the data from redis return fun.callback(shardedJedis); } catch (Exception e) { e.printStackTrace(); } finally { if (null != shardedJedis) { // Close, detect whether the connection is valid, put it back into the connection pool, and reset the status if it is invalid, shardedJedis.close(); } } return null; } /** * Execute the set operation* @param key * @param value * @return */ public String set(final String key,final String value){ return this.execute(new Function<String, ShardedJedis>() { @Override public String callback(ShardedJedis e) { return e.set(key, value); } }); } /** * Perform a set operation and set the survival time in seconds* @param key * @param value * @param seconds * @return */ public String set(final String key,final String value,final Integer seconds){ return this.execute(new Function<String, ShardedJedis>() { @Override public String callback(ShardedJedis e) { String str =e.set(key, value); e.expire(key, seconds); return str; } }); } /** * Perform get operation* @param key * @return */ public String get(final String key){ return this.execute(new Function<String, ShardedJedis>() { @Override public String callback(ShardedJedis e) { return e.get(key); } }); } /** * Perform set operation* @param key * @return */ public Long del(final String key){ return this.execute(new Function<Long, ShardedJedis>() { @Override public Long callback(ShardedJedis e) { return e.del(key); } }); } /** * Set the survival time in seconds* @param key * @param seconds * @return */ public Long expire(final String key, final Integer seconds) { return this.execute(new Function<Long, ShardedJedis>() { @Override public Long callback(ShardedJedis e) { return e.expire(key, seconds); } }); }}); }}Step 5: Start the redis service, redis-server.exe, and double-click to open:
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.