用户工具

站点工具


分享:技术:redis:spring整合redis

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

后一修订版
前一修订版
分享:技术:redis:spring整合redis [2015/07/20 09:34]
gxx 创建
分享:技术:redis:spring整合redis [2015/07/20 09:48] (当前版本)
gxx
行 12: 行 12:
  <​version>​2.4.2</​version>​  <​version>​2.4.2</​version>​
 </​dependency>​ </​dependency>​
 +</​code>​
 +===== 引入placeholder =====
 +<code xml application-context.xml>​
 +<!-- 数据库配置文件位置 -->
 +<​context:​property-placeholder location="​classpath:/​redis.properties"​ />
 +</​code>​
 +===== application-cache-redis.xml =====
 +<code xml application-cache-redis.xml>​
 +<?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>​
 +</​code>​
 +===== redis.properties =====
 +<code txt redis.properties>​
 +# 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
 +</​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>​ </​code>​
分享/技术/redis/spring整合redis.1437356075.txt.gz · 最后更改: 2015/07/20 09:34 由 gxx