Redis學習筆記(5)-SpringDataRedis的使用

來源:互聯網
上載者:User
與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基礎資料型別 (Elementary Data Type)

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值的綁定放在獲得介面時了,此舉方便了每次操作基礎資料型別 (Elementary Data Type)的時候不用反覆的去填寫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就是你要監聽的頻道。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.