First of all, the installation of redis is not described in detail, mine is installed on ubuntu16.0.4
Then start redis cd to the installation directory of redis and then start redis.server redis.conf,
I didn't seem to be able to execute only redis.server before,
Remember to enable remote access in the redis.conf configuration
test was successful
Let's start the combination of our redis and spring
maven configuration import
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.0.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.3</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.10</version> </dependency>
jar包
spring-redis file configuration
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.guo.*" annotation-config="true" /> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="10"></property> <property name="maxIdle" value="10"></property> <property name="minIdle" value="2"></property> <property name="maxWaitMillis" value="15000"></property> <property name="minEvictableIdleTimeMillis" value="300000"></property> <property name="numTestsPerEvictionRun" value="3"></property> <property name="timeBetweenEvictionRunsMillis" value="60000"></property> <property name="testOnBorrow" value="true"></property> <property name="testOnReturn" value="true"></property> <property name="testWhileIdle" value="true"></property> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"> <property name="hostName" value="192.168.1.112" /> <property name="port" value="6379" /> <property name="timeout" value="15000" /> <property name="database" value="0" /> <property name="usePool" value="true" /> <property name="poolConfig" ref="jedisPoolConfig" /> </bean> <!-- redis template definition p indicates that the properties in the bean are injected, the format is p: property name=injected object The effect is the same as using the <property> tag in the bean --> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="jedisConnectionFactory"> <!-- StringRedisSerializer is recommended for key/hashKey serialization. --> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> </bean> <!-- Encapsulation of string operations --> <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="jedisConnectionFactory" /> <!-- <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" c:template-ref="redisTemplate"/> --> </beans>
Then there is the application in the service
@Resource(name="redisTemplate") RedisTemplate redisTemplate;
@Override public User getUser(final String id) { ValueOperations<String, User> valueops = redisTemplate .opsForValue (); User user = valueops.get(id); return user; } @Override public void saveUser( User user) { // redisTemplate.opsForValue().set("guo", "ok"); // redisTemplate.execute(new RedisCallback<Object>() { // // @Override // public Object doInRedis(RedisConnection connection) throws DataAccessException { // connection.set(redisTemplate.getStringSerializer().serialize("user.uid." + user.getUserid()), // redisTemplate.getStringSerializer().serialize(user.getProfilephoto())); // return null; // } // }); // ValueOperations<String, User> valueops = redisTemplate.opsForValue(); ValueOperations<String, User> valueops = redisTemplate.opsForValue(); valueops.set(user.getUserid(), user); }
storage of list
@Override public void saveUserList() { // redisTemplate.opsForValue().set("guo", "ok"); // redisTemplate.execute(new RedisCallback<Object>() { // // @Override // public Object doInRedis(RedisConnection connection) throws DataAccessException { // connection.set(redisTemplate.getStringSerializer().serialize("user.uid." + user.getUserid()), // redisTemplate.getStringSerializer().serialize(user.getProfilephoto())); // return null; // } // }); // ValueOperations<String, User> valueops = redisTemplate.opsForValue(); List<User> list=new ArrayList<User>(); for(int i=0;i<=5;i++){ User user=new User(); user.setUserid(""+i); list.add(user); } ListOperations<String, List> valueops = redisTemplate.opsForList(); valueops.leftPush("list",list); } @Override public List<User> getUserList() { // redisTemplate.opsForValue().set("guo", "ok"); // redisTemplate.execute(new RedisCallback<Object>() { // // @Override // public Object doInRedis(RedisConnection connection) throws DataAccessException { // connection.set(redisTemplate.getStringSerializer().serialize("user.uid." + user.getUserid()), // redisTemplate.getStringSerializer().serialize(user.getProfilephoto())); // return null; // } // }); // ValueOperations<String, User> valueops = redisTemplate.opsForValue(); ListOperations<String, List> valueops = redisTemplate.opsForList(); List<User> list= valueops.leftPop("list"); return list; }
redis.opsForValue() encapsulates operation strings
redis.opsForList() encapsulates the operation list
redis.opsForSet() encapsulates operation sets
redis.opsForZSet() encapsulates the operation sorted sets
redis.opsForHash() encapsulates operation hashs
Others will be added in practice