springdata+redis配置详解
springdata设计初衷是位简化数据类型和数据的持久化存储,它并不局限是关系型数据库还是nosql数据库,都提供了简化的数据库连接,让数据获取变得更加的简单。所有这些的实现有统一的api提供。
本文主要设置spring-data-redis的相关配置特性:
1.RedisTemplate:高度封装的,自动连接池管理类;
2.对数据类型进行了归类,封装了操作接口类:
a) ValueOperations:key-value操作
b) setOperations:set的相关操作
c) ZsetOperations:
d) HashOperations:hash数据类型操作
e) ListOperations:list数据类型操作
3.对事务进行 封装,通过容器进行控制。
4.序列化机制,提供各种序列化策略选择。
集成配置详解:
1.提供简单封装保存查询操作接口,以及实现类。
public interface RedisCommand { public void save(String key,String value); public String get(String key); public <T> T getObject(String key,Class<T> clazz); }
@Service
public class RedisHandle implements RedisCommand { @Autowired
protected RedisTemplate<String, String> redisTemplate; @Override
public void save(final String key, final String value) {
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
connection.set(
redisTemplate.getStringSerializer().serialize(
"user.uid." + key), redisTemplate
.getStringSerializer().serialize(value));
return null;
}
});
} @Override
public String get(final String key) {
return redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] keys = redisTemplate.getStringSerializer().serialize(
"user.uid." + key);
if (connection.exists(keys)) {
byte[] value = connection.get(keys);
String name = redisTemplate.getStringSerializer()
.deserialize(value);
return name;
}
return null;
}
});
} @Override
public <T> T getObject(String key, Class<T> clazz) {
// TODO Auto-generated method stub
return null;
} }
2.业务逻辑类
@Service(value="userRedis")
public class UserRedisImpl implements UserRedis{ @Autowired
protected RedisHandle handler; @Override
public void saveUser(final User user) {
handler.save(user.getId()+"", JSONObject.toJSONString(user));
} @Override
public User getUser(final long id) {
String value =handler.get(id+"");
User u= JSONObject.parseObject(value, User.class);
return u;
}
}
3.测试类:
public class TestUseRedis { @SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/spring-redis.xml");
UserRedis userDAO = (UserRedis)ac.getBean("userRedis");
System.out.println("userDao"+JSONObject.toJSONString(userDAO));
User user1 = new User();
user1.setId(4);
user1.setName("oba2sssss220a");
userDAO.saveUser(user1);
User user2 = userDAO.getUser(2);
System.out.println(user2.getName());
}
}
4.配置文件相关:
spring-redis:
<?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"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:redis.properties" />
<context:component-scan base-package="com.hoo.report.web.service">
</context:component-scan>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<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.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean> </beans>
pom引用:
<!-- spring data -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.0.2.RELEASE</version>
</dependency> <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency>
redis.properties:
redis.host=127.0.0.1
redis.port=6379
redis.pass= redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
springdata+redis配置详解的更多相关文章
- redis配置详解
##redis配置详解 # Redis configuration file example. # # Note that in order to read the configuration fil ...
- redis 配置详解
# redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...
- redis配置详解(中英文)
V2.8.21: (中英字幕同步) # Redis configuration file example#* Redis 配置文件例子 # Note on units: when memory siz ...
- Redis学习——详解Redis配置文件(三)
一.Redis脚本简介 在我们介绍Redis的配置文件之前,我们先来说一下Redis安装完成后生成的几个可执行文件: redis-server .redis-cli .redis-benchmark ...
- redis配置文件详解-3
redis3.0以上配置文件 #################################INCLUDES ################################### include ...
- redis.conf 配置详解
# Redis 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, 5GB, 4M 等类似的格式,其转换方式如下(不区分大小写) # # 1k => 1000 bytes # 1kb = ...
- redis.conf 具体配置详解
redis.conf 具体配置详解 # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => ...
- redis.windows.conf配置详解
redis.windows.conf配置详解 转自:https://www.cnblogs.com/kreo/p/4423362.html # redis 配置文件示例 # 当你需要为某个配置项指定内 ...
- redis cluster 集群 安装 配置 详解
redis cluster 集群 安装 配置 详解 张映 发表于 2015-05-01 分类目录: nosql 标签:cluster, redis, 安装, 配置, 集群 Redis 集群是一个提供在 ...
随机推荐
- vcredist作用
一.vcredist作用: vcredist_x86.exe是微软公司Visual C++的32位运行时库,包含了一些Visual C++的库函数. vcredist_x64.exe是微软公司Visu ...
- 网络抓包--Wireshark
Wireshark 是一款非常棒的Unix和Windows上的开源网络协议分析器.它可以实时检测网络通讯数据,也可以检测其抓取的网络通讯数据快照文件.可以通过图形界面浏览这些数据,可以查看网络通讯数据 ...
- JavaEE Tutorials (13) - 使用锁定控制对实体数据的并发访问
13.1实体锁定和并发概述180 13.1.1使用乐观锁定18113.2锁模式181 13.2.1设置锁模式182 13.2.2使用悲观锁定183
- 51nod 1237 最大公约数之和 V3(杜教筛)
[题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1237 [题目大意] 求[1,n][1,n]最大公约数之和 ...
- 315M无线发射模块天线的长度计算
波长=光速/频率=300/315=0.952米 1/4波长须要的天线长度=波长*1/4=0.952/4=0.238米 考虑导线传播高频信号的缩短率在0.98左右,因此天线长度=0.238*0.98=0 ...
- Hadoop MapReduce链式实践--ChainReducer
版本号:CDH5.0.0,HDFS:2.3.0,Mapreduce:2.3.0,Yarn:2.3.0. 场景描写叙述:求一组数据中依照不同类别的最大值,比方,例如以下的数据: data1: A,10 ...
- Lable 控件 -- 用代码改变要显示字体的颜色
lable控件怎么改变显示字体的颜色 代码如下: string color = "#B72C34"; this.lbl.ForeColor = System.Drawing.Col ...
- Poj 3517 And Then There Was One(约瑟夫环变形)
简单说一下约瑟夫环:约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个 ...
- Matlab中的取整-floor,ceil,fix,round
FLOOR Round towards minus infinity. FLOOR(X) rounds the elements of X to the nearest integers toward ...
- objective-C学习笔记(七) 字符串处理
字符串NSString NSString 是一个Unicode编码,16位字符的字符序列. NSString 是一个类,拷贝时需要注意. 初始化方法:字面量初始化.初始化器.工厂方法. NSStrin ...