redis与spring的集成
2016-04-06 11:23
441 查看
首先需要安装redis
乌班图的安装 apt-get install redis-server
然后是修改 /etc/redis/redis.conf 文件 修改里面的 bind 127.0.0.1 改成你的外网ip这样 外网才能访问
然后/etc/init.d/redis-server restart
客户端与spring集成 需要一下jar包
http://yun.baidu.com/share/link?shareid=1006564976&uk=958682606
下载后配置applicationContext.xml spring的配置文件
加入(注意 sentinels的地址是你客户端的地址 不要与jedisConnFactory中的服务器端地址搞混)
然后再程序中可以这样调用
感觉和memached的集成差不多 但是 听说redis的功能要强大的许多,,,继续研究中
后续补充:
另一种简单的xml实现方式
实现代码map取值
乌班图的安装 apt-get install redis-server
然后是修改 /etc/redis/redis.conf 文件 修改里面的 bind 127.0.0.1 改成你的外网ip这样 外网才能访问
然后/etc/init.d/redis-server restart
客户端与spring集成 需要一下jar包
http://yun.baidu.com/share/link?shareid=1006564976&uk=958682606
下载后配置applicationContext.xml spring的配置文件
加入(注意 sentinels的地址是你客户端的地址 不要与jedisConnFactory中的服务器端地址搞混)
<bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration"> <property name="master"> <bean class="org.springframework.data.redis.connection.RedisNode"> <property name="name" value="mymaster"></property> </bean> </property> <property name="sentinels"> <set> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg index="0" value="192.168.1.18" /> <constructor-arg index="1" value="7031" /> </bean> <!-- <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg index="0" value="10.6.1**.**6" /> <constructor-arg index="1" value="7031" /> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg index="0" value="10.6.1**.**1" /> <constructor-arg index="1" value="7031" /> </bean> --> </set> </property> </bean> <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="115.XX.XXX.XX"/> <property name="port" value="6379"/> <property name="usePool" value="false"/> <constructor-arg ref="redisSentinelConfiguration"/> </bean> <bean id="RedisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnFactory" /> </bean>
然后再程序中可以这样调用
@Autowired private RedisTemplate<String, Object> redisTemplate; @RequestMapping("/selecttest") @ResponseBody public String selecttest(HttpServletRequest request) { redisTemplate.opsForValue().set("name", "周小帅"); System.out.println(redisTemplate.opsForValue().get("name")); return redisTemplate.opsForValue().get("name"); }
感觉和memached的集成差不多 但是 听说redis的功能要强大的许多,,,继续研究中
后续补充:
另一种简单的xml实现方式
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true" p:host-name="IP" p:port="6379" p:password="XXXX" /> <!-- redis template definition --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory"/>
实现代码map取值
package com.controller; import java.io.IOException; import java.net.URLDecoder; import java.net.URLEncoder; import java.text.ParseException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.model.Cat; import com.model.User; import com.service.TestService; import com.util.nuozhengtong.idcard.Demo; @Controller public class TestController { @Autowired private TestService testService; @Autowired private RedisTemplate<String, Object> redisTemplate; @RequestMapping("/index") public ModelAndView index(HttpServletRequest request, HttpServletResponse response, HttpSession session, String pageNo,String pageCount, String name, ModelMap modelMap) throws IOException, ParseException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); if (pageNo == null || pageNo.equals("") || pageCount == null || pageCount.equals("")) { pageNo = "1"; pageCount = "10"; } if ((Integer.parseInt(pageNo)-1)*Integer.parseInt(pageCount)>210) { pageNo="21"; pageNo="10"; } if (name!=null&&!name.equals("")) { if (name.contains("%")) { modelMap.put("name", URLEncoder.encode(name, "UTF-8")); name=URLDecoder.decode(URLDecoder.decode(name, "UTF-8"), "UTF-8"); }else { modelMap.put("name", URLEncoder.encode(URLEncoder.encode(name, "UTF-8"), "UTF-8")); } modelMap.put("yname", URLDecoder.decode(name, "UTF-8")); }else{ modelMap.put("name", ""); name=""; } Object redisname=redisTemplate.opsForHash().entries(name);//取map值 if (redisname!=null) { modelMap.putAll( (Map<String, Object>)redisname); return new ModelAndView("/news", modelMap); } modelMap.put("pageNo", Integer.parseInt(pageNo)); modelMap.put("pageCount", Integer.parseInt(pageCount)); Long totleCount = testService.getCatsByUserCount(name);//TODO if (totleCount % 10 != 0) { totleCount = totleCount / 10 + 1; } else { totleCount = totleCount / 10; } List<Integer> totleCountList = new ArrayList<Integer>(); for (int i = 0; i < totleCount; i++) { totleCountList.add(i + 1); } modelMap.put("totleCountList", totleCountList); List<Map> list = testService.getCatsByUser(name,pageNo,pageCount);//TODO modelMap.put("list", list); redisTemplate.opsForValue().set(name, modelMap.toString(), 10); return new ModelAndView("/news", modelMap); } }
相关文章推荐
- redis开机启动
- PHP下使用Redis消息队列发布微博(复制)
- Redis三种启动方式
- Redis配置文件参数详解
- Linux下安装Redis
- Linux下PHP安装Redis扩展
- Python连接Redis连接配置
- (转)LINUX测试环境部署Redis(四)
- Redis启动项Config的配置
- Spring Data Redis—Pub/Sub(附Web项目源码) (转)
- 如何解决在centos上面用yum不能安装redis
- 在centos6.3用yum安装redis
- Redis
- 部署 Redis 群集
- redis(四))——多实例化——实现主从配置
- Azure Redis
- redis(三)--Java代码-telnet可以连接成功但是出现Connect timeout问题-解决方案
- Redis未授权访问事故记录
- redis持久化
- MISCONF Redis is configured to save RDB snapshots