SpringData Redis的简单使用
SpringDate Redis是在Jedis框架的基础之上对Redis进行了高度封装,通过简单的属性配置就可以通过调用方法完成对Redis数据库的操作,而且SpringData Redis使用了连接工厂(JedisConnectionFactory),相比于Jedis框架性能更好。
Spring集成Jedis要使用的jar包
<!-- Jedis 与spring整合 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.5.1</version>
</dependency>
在xml中的配置
<!-- Redis 连接池 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最小保持idle状态的对象数 -->
<property name="minIdle" value="20"/>
<!-- 最大能够保持idel状态的对象数-->
<property name="maxIdle" value="50"/>
<!-- 最大分配的对象数 -->
<property name="maxTotal" value="2048"/>
<!-- 当池内没有返回对象时,最大等待时间 -->
<property name="maxWaitMillis" value="3000"/>
<!-- 当调用borrow Object方法时,是否进行有效性检查 -->
<property name="testOnBorrow" value="true"/>
</bean>
<!-- business Redis 连接工厂 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
destroy-method="destroy">
<!-- Redis地址 -->
<property name="hostName" value="127.0.0.1"/>
<!-- Redis端口 -->
<property name="port" value="6379"/>
<!-- Redis库索引 -->
<property name="database" value="0"/>
<!-- Redis连接池 -->
<property name="poolConfig" ref="poolConfig"/>
<property name="usePool" value="true"/>
<!-- 连接redis的密码 -->
<property name="password" value="123456"/>
</bean>
<!--针对String的序列化工具-->
<bean id="stringRedisSerializer" name="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<!-- SpringData Redis默认使用的序列化工具 -->
<bean id="jdkSerializationRedisSerializer" name="jdkSerializationRedisSerializer"
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<!-- RedisTemplate类配置 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="hashKeySerializer" ref="stringRedisSerializer"/>
<property name="hashValueSerializer" ref="jdkSerializationRedisSerializer"/>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
</bean>
通过使用RedisTemplate类的opsForValue()方法获取封装操作value为字符串的各种命令的方法的操作对象、opsForList()方法获取封装操作value为列表的各种命令的方法的操作对象、opsForSet()方法获取封装操作value为集合的各种命令的方法的操作对象、opsForHash()方法获取封装操作value为字符串的各种命令的方法的操作对象、opsForZSet()方法获取封装操作value为有序集合的各种命令的方法的操作对象、opsForHyperLogLog()方法获取封装操作基数统计的各种命令的方法的操作对象。通过获取的操作对象,直接调用封装的方法对Redis数据库进行操作。
备注:Redis是String类型的key-value类型的nosql数据库,对于对象,我们可以将在value里存储的对象进行json序列化,然后储存在Redis中,其中要使用到json序列化工具,SpringData Redis提供了json序列化工具类(JacksonJsonRedisSerializer<T>),使用过程中遇到了一点麻烦,参考网上的事例自己写了一个json序列化工具类,代码如下
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping; public class MyJacksonJsonRedisSerializer implements RedisSerializer<Object> { private final ObjectMapper mapper; public MyJacksonJsonRedisSerializer() {
this((String) null);
} public MyJacksonJsonRedisSerializer(String classPropertyTypeName) { this(new ObjectMapper()); if (StringUtils.hasText(classPropertyTypeName)) {
mapper.enableDefaultTypingAsProperty(DefaultTyping.NON_FINAL,
classPropertyTypeName);
} else {
mapper.enableDefaultTyping(DefaultTyping.NON_FINAL, As.PROPERTY);
}
} public MyJacksonJsonRedisSerializer(ObjectMapper mapper) { Assert.notNull(mapper, "ObjectMapper must not be null!");
this.mapper = mapper;
} @Override
public byte[] serialize(Object source) throws SerializationException { if (source == null) {
return new byte[0];
} try {
return mapper.writeValueAsBytes(source);
} catch (JsonProcessingException e) {
throw new SerializationException("Could not write JSON: "
+ e.getMessage(), e);
}
} @Override
public Object deserialize(byte[] source) throws SerializationException {
return deserialize(source, Object.class);
} public <T> T deserialize(byte[] source, Class<T> type)
throws SerializationException { Assert.notNull(
type,
"Deserialization type must not be null! Pleaes provide Object.class to make use of Jackson2 default typing."); if (source == null || source.length == 0) {
return null;
} try {
return mapper.readValue(source, type);
} catch (Exception ex) {
throw new SerializationException("Could not read JSON: "
+ ex.getMessage(), ex);
}
}
}
SpringData Redis的简单使用的更多相关文章
- redis 的简单命令
以下实例讲解了如何启动 redis 客户端: 启动 redis 客户端,打开终端并输入命令 redis-cli.该命令会连接本地的 redis 服务. $redis-cli redis > re ...
- Redis的简单了解以及主从复制
1.Redis的简单了解 Redis是一种高性能的分布式NoSql数据库,持久存储,高并发,数据类型丰富,通过现场申请内存空间,同时可以配置虚拟内存.五种数据类型:string(字符串,这种格式和me ...
- Redis主从复制简单介绍
由于本地环境的使用,所以搭建一个本地的Redis集群,本篇讲解Redis主从复制集群的搭建,使用的平台是Windows,搭建的思路和Linux上基本一致! (精读阅读本篇可能花费您15分钟,略读需5分 ...
- Redis 的简单运算
Redis 的简单运算 命令 说明 备注 incr key 在原字段上加 1 只能对整数操作 incrby key increment 在原字段上加上整数 (increment) 只能对整数操作 de ...
- python redis 实现简单的消息订阅
python + redis 实现简单的消息订阅 订阅端 import redis from functools import wraps class Subscribe: def __init__( ...
- springdata+redis配置详解
springdata设计初衷是位简化数据类型和数据的持久化存储,它并不局限是关系型数据库还是nosql数据库,都提供了简化的数据库连接,让数据获取变得更加的简单.所有这些的实现有统一的api提供. 本 ...
- Redis的简单动态字符串实现
Redis 没有直接使用 C 语言传统的字符串表示(以空字符结尾的字符数组,以下简称 C 字符串), 而是自己构建了一种名为简单动态字符串(simple dynamic string,sds)的抽象类 ...
- Redis——分布式简单使用
Redis简介:Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis安装:参考博客http://www ...
- Redis的简单介绍及在Windows下环境搭建
简单介绍 1,Redis是什么 最直接的还是看官方的定义吧. Redis is an open source (BSD licensed), in-memory data structure stor ...
随机推荐
- @suppressWarnings("unchecked") java 中是什么意思 (一般放dao查询方法上)
J2SE 提供的最后一个批注是 @SuppressWarnings.该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默. 一点背景:J2SE 5.0 为 Java 语言增加 ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十四)之Type Information
Runtime type information (RTTI) allow you to discover and use type information while a program is ru ...
- 给我Python几十行代码,我还你一个微信聊天助手
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 故事胶片 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...
- Video tagging systems based on DNNs
Need: With the ever-growth large-scale video in the mobile phone, so what will everyone get from the ...
- O - Employment Planning HDU - 1158
题目大意: 第一行一个n,表示共n个月份,然后第二行分别表示一个工人的聘请工资,月薪水,解雇工资.第三行是n个月每个月需要的工人的最少数目.然后求最少花费 题解: dp[i][j] 表示第i个月聘请j ...
- ppt和pptx转图片完整代码,解决2003版和2007版中文乱码问题
引入所需依赖,注意poi版本,新版本不支持,最好使用和我一样的版本. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --& ...
- Python数据分析:大众点评数据进行选址
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:砂糖侠 如果你处于想学Python或者正在学习Python,Pyth ...
- WANNACRY病毒中的加密技术分析
WANNACRY病毒中的加密技术分析 2019/11/6 16:56:46 分析WANNACRY病毒中的加解密技术的应用.分析内容包括但不限于:对称密码技术和公钥密码技术的作用:受害者支付赎金后就会恢 ...
- 创建线程的方式三:实现Callable接口-----JDK5.0 新增
package com.yhqtv.java2; /* * 创建线程的方式三:实现Callable接口-----JDK5.0 新增 * * 如何理解实现Callable接口的方式创建多线程比实现Run ...
- 理解分布式一致性:Paxos协议之Generalized Paxos & Byzantine Paxos
理解分布式一致性:Paxos协议之Generalized Paxos & Byzantine Paxos Generalized Paxos Byzantine Paxos Byzantine ...