POM:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.yzl</groupId>
<artifactId>redis.first</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<!-- 2.7.1在和spring的spring-data-redis 1.4.1集成时会报找不到方法的错,原因是JedisShardInfo类的属性由timeout变成了soTimeout了 -->
<!-- <version>2.7.1</version> -->
<version>2.6.2</version>
</dependency> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.0.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.0.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
</dependencies>
</project>

spring配置文件:

 <?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:ss="http://www.springframework.org/schema/security"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:property-placeholder location="classpath:redis-pool.properties"/> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" /> <bean class="redis.clients.jedis.JedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1" value="${redis.ip}" />
<constructor-arg index="2" value="${redis.port}" />
</bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!-- 具体可配置项可以参见此类的源码 -->
<property name="hostName" value="${redis.ip}" />
</bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean> <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
</beans>

测试类:

 package com.yzl;

 import java.io.Serializable;

 import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate; /**
* RedisApp之spring redisTemplate的测试类
*
* @author yangzhilong
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class RedisApp3Test {
private ApplicationContext app;
private RedisTemplate<Serializable, Serializable> redisTemplate;
private StringRedisTemplate stringRedisTemplate; @Before
public void before(){
app = new ClassPathXmlApplicationContext("spring-config.xml");
redisTemplate = (RedisTemplate)app.getBean("redisTemplate");
stringRedisTemplate = app.getBean(StringRedisTemplate.class);
} @Test
public void test(){
System.out.println("use StringRedisTemplate save value");
//保存
stringRedisTemplate.execute(new RedisCallback<String>() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
connection.set(stringRedisTemplate.getStringSerializer().serialize("name"), stringRedisTemplate.getStringSerializer().serialize("hello"));
return null;
}
}); //取值
String value = stringRedisTemplate.execute(new RedisCallback<String>() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
return stringRedisTemplate.getStringSerializer().deserialize(connection.get(stringRedisTemplate.getStringSerializer().serialize("name")));
} }); System.out.println("use StringRedisTemplate get value :" + value); System.out.println("use RedisTemplate append value");
//追加
redisTemplate.execute(new RedisCallback<String>() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
connection.append(redisTemplate.getStringSerializer().serialize("name"), redisTemplate.getStringSerializer().serialize(" redis"));
return null;
}
}); //取值
value = redisTemplate.execute(new RedisCallback<String>() { public String doInRedis(RedisConnection connection) throws DataAccessException {
// TODO Auto-generated method stub
return redisTemplate.getStringSerializer().deserialize(connection.get(redisTemplate.getStringSerializer().serialize("name")));
}
});
System.out.println("use RedisTemplate get value :" + value);
} @After
public void after(){
System.out.println("end~~~");
}
}

运行结果:

 use StringRedisTemplate save value
2015-08-14 15:23:27,241 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Opening RedisConnection
2015-08-14 15:23:27,379 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Closing Redis Connection
2015-08-14 15:23:27,381 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Opening RedisConnection
2015-08-14 15:23:27,382 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Closing Redis Connection
use StringRedisTemplate get value :hello
use RedisTemplate append value
2015-08-14 15:23:27,382 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Opening RedisConnection
2015-08-14 15:23:27,404 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Closing Redis Connection
2015-08-14 15:23:27,405 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Opening RedisConnection
2015-08-14 15:23:27,405 [org.springframework.data.redis.core.RedisConnectionUtils]-[DEBUG] Closing Redis Connection
use RedisTemplate get value :hello redis
end~~~

下一篇:7、redis之使用spring集成commons-pool来操作常见数据类型

6、redis之使用spring-data-redis的Template的更多相关文章

  1. spring mvc Spring Data Redis RedisTemplate [转]

    http://maven.springframework.org/release/org/springframework/data/spring-data-redis/(spring-data包下载) ...

  2. Spring Data Redis简介以及项目Demo,RedisTemplate和 Serializer详解

    一.概念简介: Redis: Redis是一款开源的Key-Value数据库,运行在内存中,由ANSI C编写,详细的信息在Redis官网上面有,因为我自己通过google等各种渠道去学习Redis, ...

  3. Spring Data Redis实现消息队列——发布/订阅模式

    一般来说,消息队列有两种场景,一种是发布者订阅者模式,一种是生产者消费者模式.利用redis这两种场景的消息队列都能够实现. 定义:生产者消费者模式:生产者生产消息放到队列里,多个消费者同时监听队列, ...

  4. 【原创】Spring Data Redis <=2.0.3反序列化漏洞

    Spring Data Redis隶属于Spring Data家族, 提供简单易用的方式来访问Redis缓存. Spring Data Redis在往Redis里面写数据的时候,默认会先对数据进行序列 ...

  5. spring data redis RedisTemplate操作redis相关用法

    http://blog.mkfree.com/posts/515835d1975a30cc561dc35d spring-data-redis API:http://docs.spring.io/sp ...

  6. Spring Data Redis—Pub/Sub(附Web项目源码)

    一.发布和订阅机制 当一个客户端通过 PUBLISH 命令向订阅者发送信息的时候,我们称这个客户端为发布者(publisher). 而当一个客户端使用 SUBSCRIBE 或者 PSUBSCRIBE ...

  7. Spring data redis的一个bug

    起因 前两天上线了一个新功能,导致线上业务的缓存总是无法更新,报错也是非常奇怪,redis.clients.jedis.exceptions.JedisConnectionException: Unk ...

  8. Spring Data Redis—Pub/Sub(附Web项目源码) (转)

    一.发布和订阅机制 当一个客户端通过 PUBLISH 命令向订阅者发送信息的时候,我们称这个客户端为发布者(publisher). 而当一个客户端使用 SUBSCRIBE 或者 PSUBSCRIBE ...

  9. spring data redis 理解

    前言 Spring Data Redis project,应用了Spring概念来开发使用键值形式的数据存储的解决方案.我们(官方)提供了一个 "template" ,这是一个高级 ...

  10. Spring Data Redis 详解及实战一文搞定

    SDR - Spring Data Redis的简称. Spring Data Redis提供了从Spring应用程序轻松配置和访问Redis的功能.它提供了与商店互动的低级别和高级别抽象,使用户免受 ...

随机推荐

  1. 架构模式逻辑层模式之:表模块(Table Model)

    表模块和领域模型比,有两个显著区别: 1:表模块中的类和数据库表基本一一对应,而领域模型则无此要求: 2:表模块中的类的对象处理表中的所有记录,而领域模型的一个对象代表表中的一行记录: 一般情况下,我 ...

  2. osx的du以字节计算

    https://stackoverflow.com/questions/5794437/difference-in-size-shown-by-du-command-and-get-info-on-m ...

  3. 指定文件兼容性模式 < meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" />的意义

    X-UA-Compatible是神马? X-UA-Compatible是IE8的一个专有<meta>属性,它告诉IE8采用何种IE版本去渲染网页,在html的<head>标签中 ...

  4. SharePoint SPListItem 权限设置

    namespace Microsoft.SharePoint { using System; using System.Text; using System.Collections.Generic; ...

  5. Eclipse系列:如何设置Eclipse关联JDK源码和文档

    一.设置Eclipse关联JDK源码 1.打开Eclipse-->Windows-->Preferences       2. 在弹出的Preferences对话框中,Java--> ...

  6. MFC中打印对话框CPrintDialog类

    void CCPrintDialogView::OnPrint() { DWORD dwflags=PD_ALLPAGES|PD_NOPAGENUMS|PD_USEDEVMODECOPIES|PD_S ...

  7. python Selenium+phantomjs 小技巧

    1.元素模糊定位 如抓取下面列表: elements = doc("li[id^='result_']") 2.元素精确定位 elements =doc("div[cla ...

  8. Android实现录音的方法(最重要的是对MediaRecorder的试用方法)

    package cn.eoe.record; import java.io.File; import java.io.IOException; import android.app.Activity; ...

  9. php随机生成汉字实现方法

    GB 2312-80 是中国国家标准简体中文字符集,全称<信息交换用汉字编码字符集·基本集>,由中国国家标准总局发布,1981年5月1日实施.GB2312 编码通行于中国大陆:新加坡等地也 ...

  10. 梯度消失(vanishing gradient)与梯度爆炸(exploding gradient)问题

    (1)梯度不稳定问题: 什么是梯度不稳定问题:深度神经网络中的梯度不稳定性,前面层中的梯度或会消失,或会爆炸. 原因:前面层上的梯度是来自于后面层上梯度的乘乘积.当存在过多的层次时,就出现了内在本质上 ...