首先須要安裝redisjava
烏班圖的安裝 apt-get install redis-serverweb
而後是修改 /etc/redis/redis.conf 文件 修改裏面的 bind 127.0.0.1 改爲你的外網ip這樣 外網才能訪問redis
而後/etc/init.d/redis-server restartspring
客戶端與spring集成 須要一下jar包服務器
http://yun.baidu.com/share/link?shareid=1006564976&uk=958682606session
下載後配置applicationContext.xml spring的配置文件app
加入(注意 sentinels的地址是你客戶端的地址 不要與jedisConnFactory中的服務器端地址搞混)ui
<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>
而後再程序中能夠這樣調用.net
@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的功能要強大的許多,,,繼續研究中rest
後續補充:
另外一種簡單的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); } }