spring data redis试用

来源:互联网 发布:数据驱动安全2.0 编辑:程序博客网 时间:2024/05/17 23:08

这几天因为要做个timeline,查到spring data有支持这个的例子,研究了下spring data redis,记录下相关过程备忘

spring data redis主页有不少链接,其中一个重要链接是http://static.springsource.org/spring-data/data-redis/docs/current/reference/html/,也有pdf版本的:http://static.springsource.org/spring-data/data-redis/docs/current/reference/pdf/spring-data-redis-reference.pdf

其实看了这个文件基本就会操作spring data redis了,也能理解其原理,下面是我的一个配置实现。

首先是pom.xml引入相关依赖:

<repositories><repository><id>spring-release</id><name>Spring Maven RELEASE Repository</name><url>http://maven.springframework.org/release</url></repository></repositories>

<dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.0.4.RELEASE</version></dependency>

在web.xml中添加redis配置文件的位置

<context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring/root-context.xml,/WEB-INF/spring/applicationContext-redis.xml</param-value></context-param>

/WEB-INF/spring/applicationContext-redis.xml文件内容:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"/><!-- Configurer that replaces ${...} placeholders with values from a properties file --><context:property-placeholder location="classpath:redis.properties"/><context:annotation-config /><context:component-scan base-package="org.springframework.data.redis.samples"/><bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="connectionFactory"/></beans>

其中提到的redis.properties文件内容如下:

# Redis settingsredis.host=192.168.3.84redis.port=6379redis.pass=

这几个值对应applicationContext-redis.xml的:

p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}

配置就是上面这些,最后是在代码里边引入:

@Autowiredprivate StringRedisTemplate redisTemplate;//......@RequestMapping(value = "/", method = RequestMethod.GET)public String home(Locale locale, Model model) {logger.info("Welcome home! the client locale is "+ locale.toString());Date date = new Date();DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);String formattedDate = dateFormat.format(date);model.addAttribute("serverTime", formattedDate );try{redisTemplate.opsForValue().set("fooooo", "barrrrrr");}catch(RedisConnectionFailureException e){//e.printStackTrace();System.out.println("connec failure");}return "home";}

这里使用的是StringRedisTemplate,而RedisTemplate使用出了问题也不知道怎么用,等以后需要用到时候再研究