<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.3.2.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.4.2</version> </dependency>
<!-- 数据库配置文件位置 --> <context:property-placeholder location="classpath:/redis.properties" />
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.maxTotal}" /> <property name="minIdle" value="${redis.minIdle}" /> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <!-- 如果不配置Serializer,那么存储的时候智能使用String,如果用User类型存储,那么会提示错误User can't cast to String!!! --> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> </bean> </beans>
# Redis settings redis.host=121.43.104.34 redis.port=6379 redis.pass= redis.minIdle=300 redis.maxIdle=600 redis.maxTotal=1000 redis.testOnBorrow=true
package com.gxx.record.service; import java.util.List; /** * <dl> * <dt><b>Title:</b></dt> * <dd> * redis服务接口</dd> * <dt><b>Description:</b></dt> * <dd> * <p> * none</dd> * </dl> * * @author Administrator * @version 1.0, 2015年7月19日 * @since record * */ public interface RedisService { /** * set * * @param key * @param value * @return */ public boolean set(String key, Object value); /** * get * * @param key * @return */ public Object get(String key); /** * delete * * @param key * @return */ public Object delete(String key); /** * listLeftPush * * @param key * @return */ public long listLeftPush(String key, String url); /** * listRightPush * * @param key * @return */ public long listRightPush(String key, String url); /** * listRange * * @param key * @return */ public List<Object> listRange(String key, long start, long end); /** * hashSetNx * * @param hash * @param key * @param value * @return */ public boolean hashSetNx(String hash, String key, String value); /** * hashSize * * @param hash * @return */ public long hashSize(String hash); /** * hashExist * * @param hash * @param key * @return */ public boolean hashExist(String hash, String key); }
package com.gxx.record.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import com.gxx.record.service.RedisService; /** * <dl> * <dt><b>Title:</b></dt> * <dd> * redis服务实现类 * </dd> * <dt><b>Description:</b></dt> * <dd> * <p>none * </dd> * </dl> * * @author Administrator * @version 1.0, 2015年7月19日 * @since record * */ @Service("redisService") public class RedisServiceImpl implements RedisService { @Autowired private RedisTemplate<String, Object> redisTemplate; /** * set * @param key * @param value * @return */ public boolean set(String key, Object value) { redisTemplate.opsForValue().set(key, value); return Boolean.TRUE.booleanValue(); } /** * get * @param key * @return */ public Object get(String key) { return redisTemplate.opsForValue().get(key); } /** * delete * @param key * @return */ public Object delete(String key) { Object object = redisTemplate.opsForValue().get(key); if( object!=null){ redisTemplate.delete(key); } return object; } /** * listLeftPush * @param key * @return */ public long listLeftPush(String key, String url) { long size = redisTemplate.opsForList().leftPush(key, url); return size; } /** * listRightPush * @param key * @return */ public long listRightPush(String key, String url) { long size = redisTemplate.opsForList().rightPush(key, url); return size; } /** * listRange * @param key * @return */ public List<Object> listRange(String key, long start, long end) { List<Object> list = redisTemplate.opsForList().range(key, start, end); return list; } /** * hashSetNx * @param hash * @param key * @param value * @return */ public boolean hashSetNx(String hash, String key, String value) { return redisTemplate.opsForHash().putIfAbsent(hash, key, value); } /** * hashSize * @param hash * @return */ public long hashSize(String hash) { return redisTemplate.opsForHash().size(hash); } /** * hashExist * @param hash * @param key * @return */ public boolean hashExist(String hash, String key) { return redisTemplate.opsForHash().hasKey(hash, key); } }
package com.gxx.record.dto; /** * <dl> * <dt><b>Title:</b></dt> * <dd> * 用户传输对象 * </dd> * <dt><b>Description:</b></dt> * <dd> * <p>none * </dd> * </dl> * * @author Administrator * @version 1.0, 2015年6月18日 * @since record * */ public class RedisDto extends BaseDto { private String key;//键 private String value;//值 public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
package com.gxx.record.web.redis; import javax.servlet.http.HttpServletRequest; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.gxx.record.dto.RedisDto; import com.gxx.record.service.RedisService; /** * QueryAllocateController负责查询调拨 * * @author gxx */ @Controller @RequestMapping("/redis/") public class RedisController { /** * 日志处理器 */ private final Logger logger = Logger.getLogger(RedisController.class); @Autowired private RedisService redisService; @RequestMapping(value = "/preRedisFtl", method = RequestMethod.GET) public String preRedisFtl() { return "redis/preRedisFtl"; } /** * 设置 * @param request * @param redisDto * @return */ @RequestMapping(value = "/set",produces="application/json") public @ResponseBody RedisDto set(HttpServletRequest request, RedisDto redisDto) { logger.info("设置:键=[" + redisDto.getKey() + "],值=[" + redisDto.getValue() + "]"); /** * 1.设置值 */ boolean result = redisService.set(redisDto.getKey(), redisDto.getValue()); /** * 2.返回结果 */ redisDto.setSuccess(result); redisDto.setMessage(result?"设置成功!":"设置失败!"); return redisDto; } /** * 获取值 * @param request * @param redisDto * @return */ @RequestMapping(value = "/get",produces="application/json") public @ResponseBody RedisDto get(HttpServletRequest request, RedisDto redisDto) { logger.info("获取值:键=[" + redisDto.getKey() + "]"); /** * 1.获取值 */ String obj = (String)redisService.get(redisDto.getKey()); /** * 2.返回结果 */ redisDto.setSuccess(true); redisDto.setMessage("获取成功"); redisDto.setValue(obj); return redisDto; } }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>redis页面</title> </head> <body> <form action="/record/redis/set.htm" method="post"> <table border="1"> <tr><td>键:</td><td><input type="text" name="key"/></td></tr> <tr><td>值:</td><td><input type="text" name="value"/></td></tr> <tr><td colspan="2" align="center"><input type="submit" value="设置"/></td></tr> </table> </form> <form action="/record/redis/get.htm" method="post"> <table border="1"> <tr><td>键:</td><td><input type="text" name="key"/></td></tr> <tr><td colspan="2" align="center"><input type="submit" value="获取"/></td></tr> </table> </form> </body> </html>