Redis字符串深入
字符串是 Redis 最基本的數據結構,它將以一個鍵和一個值存儲于 Redis 內部,它猶如 Java 的 Map 結構,讓 Redis 通過鍵去找到值。Redis 字符串的數據結構如下圖所示。
Redis 會通過 key 去找到對應的字符串,比如通過 key1 找到 value1,又如在 Java 互聯網中,假設產品的編號為 0001,只要設置 key 為 product_0001,就可以通過 product_0001 去保存該產品到 Redis 中,也可以通過 product_0001 從 redis 中找到產品信息。
字符串的一些基本命令。
我們看到了字符串的常用操作,為了在 Spring 中測試這些命令,首先配置 Spring 關于 Redis 字符串的運行環境,配置 Spring 關于 Redis 字符串的運行環境代碼如下所示。
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxIdle" value="50" /><property name="maxTotal" value="100" /><property name="maxWaitMillis" value="20000" /> </bean><bean id="connectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><property name="hostName" value="localhost" /><property name="port" value="6379" /><property name="poolConfig" ref="poolConfig" /> </bean> <bean id="jdkSerializationRedisSerializer"class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> <bean id="stringRedisSerializer"class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="connectionFactory" /><property name="keySerializer" ref="stringRedisSerializer" /><property name="valueSerializer" ref="jdkSerializationRedisSerializer" /> </bean>注意:這里給 Spring 的 RedisTemplate 的鍵值序列化器設置為了 String 類型,所以它就是一種字符串的操作。假設把這段 Spring 的配置代碼保存為一個獨立為文件 applicationContext.xml,使用 Spring 測試 Redis 字符串操作代碼如下所示。
package com.test;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.data.redis.core.RedisTemplate;import com.pojo.Role;public class Test {public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);// 設值redisTemplate.opsForValue().set("key1", "value1");redisTemplate.opsForValue().set("key2", "value2");// 通過key獲取值String value1 = (String) redisTemplate.opsForValue().get("key1");System.out.println(value1);// 通過key刪除值redisTemplate.delete("key1");// 求長度Long length = redisTemplate.opsForValue().size("key2");System.out.println(length);// 設值新值并返回舊值String oldValue2 = (String) redisTemplate.opsForValue().getAndSet("key2", "new_value2");System.out.println(oldValue2);// 通過key獲取值.String value2 = (String) redisTemplate.opsForValue().get("key2");System.out.println(value2);// 求子串String rangeValue2 = redisTemplate.opsForValue().get("key2", 0, 3);System.out.println(rangeValue2);// 追加字符串到末尾,返回新串長度int newLen = redisTemplate.opsForValue().append("key2", "_app");System.out.println(newLen);String appendValue2 = (String) redisTemplate.opsForValue().get("key2");System.out.println(appendValue2);} }這是主要的目的只是在 Spring 操作 Redis 鍵值對。
在 Spring 中,redisTemplate.opsForValue() 所返回的對象可以操作簡單的鍵值對,可以是字符串,也可以是對象,具體依據你所配置的序列化方案。
由于配置 Spring 關于 Redis 字符串的運行環境代碼所配置的是字符串,所以以字符串來操作 Redis。
上面介紹了字符串最常用的命令,但是 Redis 除了這些之外還提供了對整數和浮點型數字的功能。如果字符串是數字(整數或者浮點數),那么 Redis 還能支持簡單的運算。不過它的運算能力比較弱,目前版本只能支持簡單的加減法運算。
Redis支持的簡單運算
由于 Redis 的功能比較弱,所以經常會在 Java 程序中讀取它們,然后通過 Java 進行計算并設置它們的值。
使用 JDK 序列化器,那么 Redis 保存的將不會是數字而是產生異常,字符是 Redis 最基本的類型,它可以使用最多的命令。測試代碼如下所示。
/** *測試Redis運算. */ public static void testCal() {ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);redisTemplate.opsForValue().set ("i", "9");printCurrValue(redisTemplate, "i");redisTemplate.opsForValue().increment("i", 1);printCurrValue(redisTemplate,"i");redisTemplate.getConnectionFactory().getConnection().decrBy(redisTemplate.getKeySerializer().serialize("i"));printCurrValue(redisTemplate, "i");redisTemplate.getConnectionFactory().getConnection().decrBy(redisTemplate.getKeySerializer() .serialize("i"), 6);printCurrValue(redisTemplate, "i");redisTemplate.opsForValue().increment("i", 2.3);printCurrValue (redisTemplate, "i"); }/** *打印當前key的值 *@param redisTemplate spring RedisTemplate *@param key 鍵 */ public static void printCurrValue(RedisTemplate redisTemplate, String key) {String i = (String) redisTemplate.opsForValue().get(key);System.err.println(i); }注意:Spring 已經優化了代碼,所以加粗的 increment 方法可以支持長整形(long)和雙精度(double)的加法,而對于減法而言,RedisTemplate 并沒有進行支持,所以用下面的代碼去代替它:
redisTemplate.getConnectionFactory().getConnection().decrBy(redisTemplate.getKeySerializer().serialize("i"),6);通過獲得連接工廠再獲得連接從而得到底層的 Redis 連接對象。為了和 RedisTemplate 的配置保持一致,所以先獲取了其 keySerializer 屬性,對鍵進行了序列化,如果獲取結果也可以進行同樣的轉換。
當然 getConnection() 只是獲取一個 spring data redis 項目中封裝的底層對象 RedisConnection,甚至可以獲取原始的鏈接對象—— Jedis 對象,比如下面這段代碼:
Jedis jedis = (Jedis)redisTemplate.getConnectionFactory().getConnection().getNativeConnection();首先,估計是因為 Redis 的版本在更替,支持的命令會有所不一,而 Spring 提供的 RedisTemplate 方法不足以支撐 Redis 的所有命令,所以這里才會有這樣的變化。
而使用純粹的 Java Redis 的最新 API 則可以看到這些命令對應的方法,這點是大家需要注意的。其次,所有關于減法的方法,原有值都必須是整數,否則就會引發異常,如下面這段代碼,通過操作浮點數減法產生異常。
redisTemplate.opsForValue().set ("i", "8.9"); redisTemplate.getConnectionFactory().getConnection().decr( redisTemplate.getKeySerializer().serialize("i"));這些在 Java 中完全可以編譯通過,但是運行之后產生了異常,這是因為對浮點數使用了 Redis 的命令,使用 Redis 的時候需要注意這些問題。
總結
以上是生活随笔為你收集整理的Redis字符串深入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言颜色设置
- 下一篇: MySQL 修改视图