Redis学习笔记(5)-SpringDataRedis的使用
来源:互联网 发布:办公平台软件 编辑:程序博客网 时间:2024/05/19 11:44
与Spring集成
我需要哪些jar包?
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId></dependency><dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId></dependency>
如何配置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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置IP地址与端口号,连接redis服务器--> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:hostName="127.0.0.1" p:port="6379" p:usePool="true"/> <!--配置redisTemplate--> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connectionFactory-ref="jedisConnectionFactory"/> <!--配置stringRedisTemplate--> <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connectionFactory-ref="jedisConnectionFactory"/></beans>
一般情况,只需要配置RedisTemplate或StringRedisTemplate其中一个就行,常用的为StringRedisTemplate。
RedisTemplate与StringRedisTemplate的区别?
StringRedisTemplate:
public class StringRedisTemplate extends RedisTemplate<String, String> { public StringRedisTemplate() { StringRedisSerializer stringSerializer = new StringRedisSerializer(); this.setKeySerializer(stringSerializer); this.setValueSerializer(stringSerializer); this.setHashKeySerializer(stringSerializer); this.setHashValueSerializer(stringSerializer); } public StringRedisTemplate(RedisConnectionFactory connectionFactory) { this(); this.setConnectionFactory(connectionFactory); this.afterPropertiesSet(); } protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) { return new DefaultStringRedisConnection(connection); }}
从上面的代码可以看出,StringRedisTemplate继承了键值类型都为String的RedisTemplate,且使用StringRedisSerializer作为序列化工具。所以StringRedisTemplate能使用的方法,RedisTemplate都能使用,下面的例子只会展示StringRedisTemplate的方法。
StringRedisSerializer:
public class StringRedisSerializer implements RedisSerializer<String> { private final Charset charset; public StringRedisSerializer() { this(Charset.forName("UTF8")); } public StringRedisSerializer(Charset charset) { Assert.notNull(charset); this.charset = charset; } public String deserialize(byte[] bytes) { return bytes == null?null:new String(bytes, this.charset); } public byte[] serialize(String string) { return string == null?null:string.getBytes(this.charset); }}
可以看出,StringRedisSerializer使用UTF8字符集处理字符串。
使用API操作基本redis基本数据类型
spring提供哪些接口操作redis基本数据?
第一组
ValueOperations:字符串类型操作ListOperations:列表类型操作SetOperations:集合类型操作ZSetOperations:有序集合类型操作HashOperations:散列操作
第二组
BoundValueOperations:字符串类型操作BoundListOperations:列表类型操作BoundSetOperations:集合类型操作BoundZSetOperations:有序集合类型操作BoundHashOperations:散列操作
如何获得接口的实现?
第一组
ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();ListOperations<String, String> listOperations = stringRedisTemplate.opsForList();SetOperations<String, String> setOperations = stringRedisTemplate.opsForSet();ZSetOperations<String, String> zSetOperations = stringRedisTemplate.opsForZSet();HashOperations<String, Object, Object> hashOperations = stringRedisTemplate.opsForHash();
第二组
BoundValueOperations<String, String> valueOperations = stringRedisTemplate.boundValueOps("key");BoundListOperations<String, String> listOperations = stringRedisTemplate.boundListOps("key");BoundSetOperations<String, String> setOperations = stringRedisTemplate.boundSetOps("key");BoundZSetOperations<String, String> zSetOperations = stringRedisTemplate.boundZSetOps("key");BoundHashOperations<String, Object, Object> hashOperations = stringRedisTemplate.boundHashOps("key");
从上面两组实现可以发现,第二组API只是在第一组API的上面将key值的绑定放在获得接口时了,此举方便了每次操作基本数据类型的时候不用反复的去填写key值,只需要操作具体的value就行了。
具体有哪些数据操作方式,如ValueOperations的get与set,ListOperations的push与pop等可以参照:Redis学习笔记(2)-Redis数据类型。他们的方法签名与客户端redis-cli操作redis时的签名是一样的。
使用API操作消息队列
使用API前需要了解
Redis学习笔记(3)-Redis事务,过期时间,队列
如何发送消息队列?
RedisTemplate template = template.convertAndSend("channel", "message");
第一个参数为发送的消息的频道,第二个参数为消息本身。
如何接受队列中的消息?
public interface MessageDelegate { void handleMessage(String message); void handleMessage(Map message); void handleMessage(byte[] message); void handleMessage(Serializable message); void handleMessage(Serializable message, String channel);}
首先,需要一个符合MessageDelegate 接口方法签名的类,这个类是自定义的,可以使用MessageDelegate 中的一个或多个签名方式,如:
public class UserMessageDelegate { public void handleMessage(String message) { System.out.println(message); }}
得到此类后,将此类注册到消息监听容器中:
<?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:redis="http://www.springframework.org/schema/redis" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis.xsd"> <redis:listener-container> <redis:listener ref="listener" method="handleMessage" topic="chatroom"/> </redis:listener-container> <bean id="listener" class="com.hzw.redis.listener.UserMessageDelegate"/></beans>
其中,topic就是你要监听的频道。
- Redis学习笔记(5)-SpringDataRedis的使用
- redis学习记录(四)-SpringDataRedis分析
- Java-SpringDataRedis使用入门
- Python之学习笔记(redis的使用)
- 虚拟内存的使用--Redis学习笔记九
- Redis的具体使用-Redis学习笔记二
- redis学习笔记[2] redis的初始使用
- Redis学习笔记(5)
- redis的学习笔记
- Redis的学习笔记
- Redis 学习笔记八 NodeJs使用Redis
- [Redis学习笔记]-Java 使用 Redis
- Redis学习笔记之一:Redis的安装以及Redis的初步使用
- redis 学习笔记(5)-Spring与Jedis的集成
- Redis学习笔记(5)对set集合的读写
- Redis学习笔记(二)——Redis的准备
- Redis学习笔记(五)Linux下redis的安装
- redis学习笔记(10)---ubuntu下redis的安装
- golang 中sync.WaitGroup以及select简单实用
- oracle JDBC连接数据库
- java抽象类和接口的区别(自己总结,方便记忆版)
- VIJOS 1321 魔塔
- MarkDown Usage
- Redis学习笔记(5)-SpringDataRedis的使用
- Android App的设计架构:MVC,MVP,MVVM与架构经验谈
- jar包学习
- 常用vim快捷键 (未包括折叠
- cocos2dx-深度解析plist文件(一)(游戏对象的数据如何从plist创建获取)
- 创建Executor
- Hbase 单机版
- UVA-11538
- iOS 简单动画效果实现的三种方式