1.spring-data-redis如何连接到redis服务端
其中定义了两个接口org.springframework.data.redis.connection下的RedisConnection和RedisConnectionFactory工厂接口:
public interface RedisConnection extends RedisCommands {
void close() throws DataAccessException;
boolean isClosed();
Object getNativeConnection();
boolean isQueueing();
boolean isPipelined();
void openPipeline();
List<Object> closePipeline();
}
其中RedisCommands接口定义了redis支持的所有操作接口,
public interface RedisConnectionFactory extends PersistenceExceptionTranslator {
RedisConnection getConnection();
}
RedisConnectionFactory接口只定义一个获取连接的方法,而PersistenceExceptionTranslator定义了出现异常时把异常转化为spring通用的DataAccessException异常,可以供spring统一处理。
spring-data-redis通过三种方式连接:
Jedis connector
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="server" p:port="6379"/>
JRedis connector
<bean id="jredisConnectionFactory" class="org.springframework.data.redis.connection.jredis.JredisConnectionFactory"
p:host-name="server" p:port="6379"/>
RJC connector
<bean id="jredisConnectionFactory" class="org.springframework.data.redis.connection.rjc.RjcConnectionFactory" p:host-name="server" p:port="6379"/>
而spring-data-redis是统一通过RedisTemplate类给用户操作
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:use-pool="true"/>
<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"/>
示例如下:
public class Example {
// inject the actual template
@Autowired
private RedisTemplate<String, String> template;
// inject the template as ListOperations
@Autowired
private ListOperations<String, String> listOps;
public void addLink(String userId, URL url) {
listOps.leftPush(userId, url.toExternalForm());
}
}
2.序列化
通过序列化接口org.springframework.data.redis.serializer来定制自己的序列化逻辑
public interface RedisSerializer<T> {
/**
* Serialize the given object to binary data.
*
* @param t object to serialize
* @return the equivalent binary data
*/
byte[] serialize(T t) throws SerializationException;
/**
* Deserialize an object from the given binary data.
*
* @param bytes object binary representation
* @return the equivalent object instance
*/
T deserialize(byte[] bytes) throws SerializationException;
}
存到redis上都是二进制的值,现在spring-data-redis里面已经提供了几种序列化机制:
JacksonJsonRedisSerializer json格式序列化
JdkSerializationRedisSerializer jdk的序列化
OxmSerializer spring的orm的序列化
StringRedisSerializer 简单的字符串跟字节转换序列化
3.spring-data-redis的消息发布订阅处理
<bean id="messageListener" class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean class="redisexample.DefaultMessageDelegate"/>
</constructor-arg>
</bean>
<!-- and this is the message listener container... -->
<bean id="redisContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="messageListeners">
<!-- map of listeners and their associated topics (channels or/and patterns) -->
<map>
<entry key-ref="messageListener">
<bean class="org.springframework.data.redis.listener.ChannelTopic">
<constructor-arg value="chatroom">
</bean>
</entry>
</map>
</property>
</bean>
每次接收到消息,就会调用注册的listener来处理。
4.支持spring cache的处理抽象
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" c:template-ref="redisTemplate">
这样支持在本地缓存访问过的记录。
5.访问redis时支持回调,给程序员开发充分的扩展性灵活性来处理自定义的一些操作
通过扩展回调接口:
public interface RedisCallback<T> {
/**
* Gets called by {@link RedisTemplate} with an active Redis connection. Does not need to care about activating or
* closing the connection or handling exceptions.
*
* @param connection active Redis connection
* @return a result object or {@code null} if none
* @throws DataAccessException
*/
T doInRedis(RedisConnection connection) throws DataAccessException;
}
实际上redisTemplate的所有操作都是通过这个回调接口来处理的
public List<Object> exec() {
return execute(new RedisCallback<List<Object>>() {
public List<Object> doInRedis(RedisConnection connection) throws DataAccessException {
return connection.exec();
}
});
}
public void delete(K key) {
final byte[] rawKey = rawKey(key);
execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) {
connection.del(rawKey);
return null;
}
}, true);
}
public void delete(Collection<K> keys) {
final byte[][] rawKeys = rawKeys(keys);
execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) {
connection.del(rawKeys);
return null;
}
}, true);
}
回调接口代码看起来很优雅,也非常灵活,设计得很不错。
分享到:
相关推荐
sources和-javadoc文件是可选的): mvn install:install-file \-DpomFile=spring-data-oracle-nosql-x.y.z.pom \-Dfile=spring-data-oracle-nosql-x.y.z.jar \-Dsources=spring-data-oracle-nosql-x.y.z-sources....
Neo4j是一个高性能的,NOSQL图形数据库 附件是java-spring-data 调用Neo4j的接口例子
08.hbase基本概念介绍--数据库--nosql数据库.mp4
随着Nosql分布式数据库的不断出现,各种非关系型数据库的重要性日益涌现。本项目基于Spring-data的API,实现一个Spring boot连接文档数据库mongoDB,并对数据进行管理的基本功能
SpringMVC+Spring-Data+MongoDB4.0开启事务支持示例 Spring5 Spring-Data MongoDB4.0 事务 技术原创 探索使用Spring5和MongoDB4.0,如何开启事务 背景 最近项目中用到了Nosql数据库mongodb和SpringMVC框架。随着项目...
-展示柜奥普方面spring-aop 引导程序缓存——弹簧缓存数据库米巴蒂斯蔚来米娜2 nosql redis -- jedis, spring-data-redis 序列化——msgpack,protobuf 测试单位——dbunit、easymock、junit、mockito、powermock ...
计算机-mysql-基于NoSQL的视图像识别系统的设计与实现.pdf
计算机-mysql-基于NoSQL的大数据处理的研究.pdf
Spring is the most popular Java-based framework for building enterprise applications. The Spring framework provides a rich ecosystem of projects to address modern application needs, like security, ...
计算机-mysql-基于NoSQL的数据分析技术的应用研究.pdf
计算机-mysql-基于NoSQL的空间数据云存储的研究.pdf
29.3. JPA and “Spring Data” 29.3.1. Entity Classes 29.3.2. Spring Data JPA Repositories 29.3.3. Creating and Dropping JPA Databases 29.3.4. Open EntityManager in View 29.4. Using H2’s Web Console ...
Nosql开发框架NoSQL扫描和利用框架的框架NoSQL开发框架2.02b发布作者弗朗西斯·亚历山大(Francis Alexander)创建的NoSQL开发框架维基NoSQL Exploitation Framework Wiki的安装和用法-https: 特征: 第一个工具,...
This book shows you how Spring's data access framework can help you connect to either non-relational or relational databases, or a combination of the two. You'll learn how Spring Data's familiar and ...
关系数据库和NoSQL数据库操作实践 旨在让学生了解关系数据库、键值数据库、列族数据库和文档数据库的基本操作,比较同样的数据库操作在不同类型的数据库产品中的实现方法,体会其中的异同点,从而加深对关系数据库和...
NULL 博文链接:https://zhaoshijie.iteye.com/blog/1135210
弹簧安全,spring-sercurity-oauth2-自动配置,nosql,redis。
jpetstore-6-spring-data-mongodb 该项目是使用Spring Data MongoDB将原始从RDBMS / MyBatis移植到NoSQL / MongoDB的练习。 使用Maven运行: git clone ...
servers and network components located in many datacenters around the world. At this scale, small and large components fail continuously and the way persistent state is managed in the face of these ...
A-collection-of-popular-NoSQL-articles-published-on-InfoQ.com