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 集群是一个提供在 ...
随机推荐
- Ubuntu Server 14.04在VMware安装的一些事儿
这几天一直在折腾Ubuntu Server 14.04,故记录下: 安装前的准备: 1.建议安装英文版,像15.04.16.04等安装中文版时存在bug,而且中文版字体显示也有问题. 2.Ubuntu ...
- C语言负数的除法和求余运算
假定我们让 a 除以 b,商为 q,余数为 r: q = a / b; r = a % b; 这里,不妨假定 b 大于 0. 我们希望 a.b.q.r 之间维持怎样的关系呢? 1.最重的一点,我们希望 ...
- Gulp 从0开始
http://www.w3ctech.com/topic/134 (该文章有很多错误) http://markpop.github.io/2014/09/17/Gulp%E5%85%A5%E9%97 ...
- JAVA并发,锁与方法
引自:<thinking in java> synchronized void f(){/* ... */}; synchronized void g(){/* ... */}; 所有对象 ...
- HDU 2167 Pebbles
题目大意:有个N*N( 3<=N<=15 )方阵, 可从中若干个数, 使其总和最大.取数要求, 当某一个数被选, 其周围8个数都不能选. 题解:记s数组为合法状态,即没有相邻的数字同时被选 ...
- HDU 3336 Count the string
题解:利用next数组来保存前缀位置,递推求解. #include <cstdio> #include <cstring> char pat[200005]; int next ...
- Java图形化界面设计——布局管理器之CardLayout(卡片布局)
- localstroge可以在页面间传递数值;
连接地址为:http://4.suancai.sinaapp.com/localstorg/a.html 原理是,a页面设置了sessionstorge,b页面可以访问到; 并且已关闭浏览器,sest ...
- Ubuntu下远程访问MySQL数据库
MySQL远程访问的命令 格式: mysql -h主机地址 -u用户名 -p用户密码 jack@jack:~$ mysql -h192.168.5.154 -usaledata -pEnter pas ...
- css学习笔记四
广州天气变冷了,css学习笔记还是要总结. 总结: 1:几米页面静态页面主要是一列结构头部banner图,mainbody部分放文字内容和图书图片,底部是页面的版权信息 2:腾讯软件中心静态页面制作( ...