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配置详解的更多相关文章

  1. redis配置详解

    ##redis配置详解 # Redis configuration file example. # # Note that in order to read the configuration fil ...

  2. redis 配置详解

    # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...

  3. redis配置详解(中英文)

    V2.8.21: (中英字幕同步) # Redis configuration file example#* Redis 配置文件例子 # Note on units: when memory siz ...

  4. Redis学习——详解Redis配置文件(三)

    一.Redis脚本简介 在我们介绍Redis的配置文件之前,我们先来说一下Redis安装完成后生成的几个可执行文件: redis-server .redis-cli .redis-benchmark ...

  5. redis配置文件详解-3

    redis3.0以上配置文件 #################################INCLUDES ################################### include ...

  6. redis.conf 配置详解

    # Redis 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, 5GB, 4M 等类似的格式,其转换方式如下(不区分大小写) # # 1k => 1000 bytes # 1kb = ...

  7. redis.conf 具体配置详解

    redis.conf 具体配置详解 # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => ...

  8. redis.windows.conf配置详解

    redis.windows.conf配置详解 转自:https://www.cnblogs.com/kreo/p/4423362.html # redis 配置文件示例 # 当你需要为某个配置项指定内 ...

  9. redis cluster 集群 安装 配置 详解

    redis cluster 集群 安装 配置 详解 张映 发表于 2015-05-01 分类目录: nosql 标签:cluster, redis, 安装, 配置, 集群 Redis 集群是一个提供在 ...

随机推荐

  1. pyhton

    http://panda.www.net.cn/cgi-bin/check.cgi?area_domain= http://whois.chinaz.com/ beautifulsoup4 impor ...

  2. 常见iis错误之一

    1.win7配置iis 出现:HTTP 错误 403.14 - Forbidden 打开 IIS 管理器. 在“功能”视图中,双击“目录浏览”. 在“目录浏览”页上,在“操作”窗格中单击“启用”. 确 ...

  3. dp-史上最戳最长最臭代码-hdu-4733-G(x)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4733 题目大意: 定义G(x)=x⊕(x>>1).给两个由0.1.?组成的长度相同的字符 ...

  4. 基于mini2440的看门狗(裸机)

    在由单片机构成的微型计算机系统中,由于单片机的工作常常会受到来自外界电磁场的干扰,造成程序的跑飞,而陷入死循环,程序的正常运行被打断,由单片机控制的系统无法继续工作,会造成整个系统的陷入停滞状态,发生 ...

  5. Linux系统编程——进程调度浅析

    概述 操作系统要实现多进程.进程调度不可缺少. 有人说,进程调度是操作系统中最为重要的一个部分.我认为这样的说法说得太绝对了一点,就像非常多人动辄就说"某某函数比某某函数效率高XX倍&quo ...

  6. hdu 4873 ZCC Loves Intersection(大数+概率)

    pid=4873" target="_blank" style="">题目链接:hdu 4873 ZCC Loves Intersection ...

  7. docker进入容器

    进入容器的三种方式: sshd nsenter exec sshd 在容器中开启一个SSHD的服务,通过SSH的协议登录到容器中,把容器看出一个vm nsenter: nsenter包含在util-l ...

  8. flexbox 伸缩布局盒

    Flexbox(伸缩布局盒) 是 CSS3 中一个新的布局模式,为了现代网络中更为复杂的网页需求而设计. Flexbox 由 伸缩容器 和 伸缩项目 组成.通过设置元素的 display 属性为   ...

  9. CXF详细介绍

    首先介绍下CXF的拦截器:简单地说,CXF使用流水线型(或者说总线型)处理机制,它的核心是一个Bus.一个客户端的请求或者一个对客户端桩代码的调用被组织成为一个Message.同时,所有的CXF功能都 ...

  10. Android 手机上安装并运行 Ubuntu 12.04

    ubuntu.sh脚本的原地址变动了,导致下载不了,现在更新了网盘地址.小技巧:遇到一些下载失效的时候可以试一试p2p下载工具(如 easyMule.迅雷等)试一试,说不定有人分享过~* —————— ...