这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
分享:技术:redis:spring整合redis [2015/07/20 09:38] gxx |
分享:技术:redis:spring整合redis [2015/07/20 09:48] (当前版本) gxx |
||
---|---|---|---|
行 74: | 行 74: | ||
redis.testOnBorrow=true | redis.testOnBorrow=true | ||
</code> | </code> | ||
+ | ===== RedisService.java ===== | ||
+ | <code java RedisService.java> | ||
+ | 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); | ||
+ | } | ||
+ | </code> | ||
+ | ===== RedisServiceImpl.java ===== | ||
+ | <code java RedisServiceImpl.java> | ||
+ | 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); | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | ===== RedisDto.java ===== | ||
+ | <code java RedisDto.java> | ||
+ | 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; | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | ===== RedisController.java ===== | ||
+ | <code java RedisController.java> | ||
+ | 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; | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | ===== preRedisFtl.ftl ===== | ||
+ | <code html preRedisFtl.ftl> | ||
+ | <!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> | ||
+ | </code> | ||
+ | ===== 演示 ===== | ||
+ | {{ :分享:技术:redis:redis_1.png?300 |}} | ||
+ | {{ :分享:技术:redis:redis_2.png?300 |}} | ||
+ | {{ :分享:技术:redis:redis_3.png?300 |}} | ||
+ | {{ :分享:技术:redis:redis_4.png?300 |}} | ||
+ | <code> | ||
+ | 127.0.0.1:6379> keys * #查看redis所有的key | ||
+ | 1) "name" | ||
+ | 2) "\xac\xed\x00\x05t\x00\x03age" | ||
+ | 3) "\xac\xed\x00\x05t\x00\x04name" | ||
+ | 4) "\xac\xed\x00\x05t\x00\bpassword" | ||
+ | 5) "\xac\xed\x00\x05t\x00\amy_name" #这个为新增的 | ||
+ | </code> |