Redis学习笔记——SpringDataRedis的使用

x33g5p2x  于2021-12-01 转载在 Redis  
字(5.6k)|赞(0)|评价(0)|浏览(305)

与Spring集成

<dependency>
	<groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>
<?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。

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基本数据类型

第一组

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操作消息队列

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就是你要监听的频道。

相关文章

微信公众号

最新文章

更多