字符串是Redis最基本的數據結構,它將以一個鍵和一個值存儲於Redis內部,它猶如Java的Map結構,讓Redis通過鍵去找到值。Redis字符串的數據結構如圖所示:
Redis會通過key去找到對應的字符串,比如通過key1找到value1,又如在Java互聯網中,假設產品的編號爲0001,只要設置key爲product_0001,就可以通過product_0001去保存該產品到Redis中,也可以通過product_0001從redis中找到產品信息。
字符串的一些基本命令:
- set key value:設置鍵值對。
- get key:通過鍵獲取值。
- del key:通過key,刪除鍵值對。返回刪除數。
- strlen key:求key指向的字符串的長度。返回長度。
- getset key value:修改原來key的對應值,並將舊值返回。
- getrange key start end:獲取字串。
- append key value:將新的字符串value,加入到原來的key指向的字符串末,返回key指向的新字符串的長度。
這裏我們知道了字符串的常用操作,下面我們用一個小Demo來測試一下這些命令。
首先配置Spring關於Redis字符串的運行環境,代碼如下:
redisSpring-cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<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="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost" />
<property name="port" value="6379" />
<property name="password" value="123456" />
<property name="poolConfig" ref="poolConfig" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="valueSerializer" ref="stringRedisSerializer" />
</bean>
</beans>
注意,這裏給Spring的RedisTemplate的鍵值序列化設置爲String類型,所以它就是一種字符串的操作。
然後編寫測試方法:
TestRedis.java
package com.ssm.redis1.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
public class TestRedis {
public static void main(String[] args){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("redisSpring-cfg.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 appendValues2=(String)redisTemplate.opsForValue().get("key2");
System.out.println(appendValues2);
}
}
運行結果:
上面介紹了字符串最常用的命令,但是Redis除了這些之外還提供了對整數和浮點型數字的功能,如果字符串是數字(整數或者浮點數),那麼Redis還能支持簡單的運算。不過他的運算能力比較弱,目前只支持簡單的加減法運算。
- incr key:在原字段上加1,只能對整數操作。
- incrby key increment:在原字段上加上整數(increment),只能對整數操作。
- decr key:在原字段上減1,只能對整數操作。
- decrby key increment:在原字段上減去整數(increment),只能對整數操作。
- incrbyfloat key increment:在原字段上加上浮點數(increment),可以操作浮點數或者整數。
在測試過程中,如果把數設置爲浮點數,那麼incr、decr、inceby、decrby的命令都會失敗。Redis並不支持減法、乘法、除法操作,功能十分有限。
我們在用代碼來測試一下,代碼如下:
testCal.java
package com.ssm.redis1.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
public class testCal {
public static void main(String[] args){
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("redisSpring-cfg.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().decr(
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 i key值
*/
private static void printCurrValue(RedisTemplate redisTemplate, String i) {
String str=(String)redisTemplate.opsForValue().get(i);
System.out.println(str);
}
}
注意,這裏Spring已經優化了代碼,所以increment方法可以支持長整型(long)和雙精度(double)的加法,而對於減法而言,RedisTemplate並沒有支持,所以只能用下面的代碼去代替它:
redisTemplate.getConnectionFactory().getConnection().decrBy(
redisTemplate.getKeySerializer().serialize("i"),6);
通過獲得連接工廠在獲得連接從而得到底層的Redis連接對象。爲了和RedisTemplate的配置保持一致,所以先獲取了其keySerializer屬性,對鍵進行了序列化,如果獲取結果也可以進行同樣的轉換。