1. 程式人生 > >redis 字串(String) (redis學習三)

redis 字串(String) (redis學習三)

字串命令

基本命令

SET 命令

用於設定給定 key 的值。如果 key 已經儲存其他值, SET 就覆寫舊值,且無視型別。

127.0.0.1:6379> set test1 1
OK

Get 命令

用於獲取指定 key 的值。如果 key 不存在,返回 nil 。如果key 儲存的值不是字串型別,返回一個錯誤。

127.0.0.1:6379> get test1
"1"
127.0.0.1:6379> get test01
(nil)

Getrange 命令

用於獲取儲存在指定 key 中字串的子字串。字串的擷取範圍由 start 和 end 兩個偏移量決定(包括 start 和 end 在內)。

127.0.0.1:6379> set testKey "how old are you?"
OK
127.0.0.1:6379> getrange testKey 0 2
"how"
127.0.0.1:6379> getrange testKey 0 -1
"how old are you?"
127.0.0.1:6379> getrange testKey 4 6
"old"
127.0.0.1:6379> getrange testKey 4 -1
"old are you?"

Getset 命令

用於設定指定 key 的值,並返回 key 的舊值。

127.0.0.1:6379> getset test2506 123
"2506"
127.0.0.1:6379> get test2506
"123"

Mget 命令

返回所有(一個或多個)給定 key 的值。 如果給定的 key 裡面,有某個 key 不存在,那麼這個 key 返回特殊值 nil 。

127.0.0.1:6379> mget test888 test1075 test756
1) "888"
2) "1075"
3) "756"

Setex 命令

為指定的 key 設定值及其過期時間。如果 key 已經存在, SETEX 命令將會替換舊的值。

127.0.0.1:6379> setex my123 10 123
OK
127.0.0.1:6379> get my123
"123"
127.0.0.1:6379> ttl my123
(integer) 3
127.0.0.1:6379> get my123
(nil)

Setnx(SET if Not eXists) 命令

在指定的 key 不存在時,為 key 設定指定的值。

127.0.0.1:6379> setnx my123 123
(integer) 1
127.0.0.1:6379> setnx my123 123
(integer) 0

Setrange 命令

用指定的字串覆蓋給定 key 所儲存的字串值,覆蓋的位置從偏移量 offset 開始。

localhost:6379> set myKey "hollo word 123"
OK
localhost:6379> get myKey
"hollo word 123"
localhost:6379> setrange myKey 6 redis
(integer) 14
localhost:6379> get myKey
"hollo redis123"

Strlen 命令

用於獲取指定 key 所儲存的字串值的長度。當 key 儲存的不是字串值時,返回一個錯誤。

localhost:6379> get myKey
"hollo redis123"
localhost:6379> strlen myKey
(integer) 14

Mset 命令

用於同時設定一個或多個 key-value 對。

localhost:6379> mset test199 123 test1990 234
OK
localhost:6379> get test199
"123"
localhost:6379> get test1990
"234"

Msetnx 命令

用於所有給定 key 都不存在時,同時設定一個或多個 key-value 對。
同mset命令

Psetex 命令

以毫秒為單位設定 key 的生存時間。
同setex

Incr 命令

將 key 中儲存的數字值增一。
如果 key 不存在,那麼 key 的值會先被初始化為 0 ,然後再執行 INCR 操作。
如果值包含錯誤的型別,或字串型別的值不能表示為數字,那麼返回一個錯誤。
本操作的值限制在 64 位(bit)有符號數字表示之內。

localhost:6379> incr test1992
(integer) 1993
localhost:6379> get myKey
"hollo redis123"
localhost:6379> incr myKey
(error) ERR value is not an integer or out of range

Incrby 命令

將 key 中儲存的數字加上指定的增量值。
如果 key 不存在,那麼 key 的值會先被初始化為 0 ,然後再執行

INCRBY 命令

如果值包含錯誤的型別,或字串型別的值不能表示為數字,那麼返回一個錯誤。
本操作的值限制在 64 位(bit)有符號數字表示之內。

localhost:6379> incrby test1992 20
(integer) 2013

Incrbyfloat 命令

為 key 中所儲存的值加上指定的浮點數增量值。
如果 key 不存在,那麼 INCRBYFLOAT 會先將 key 的值設為 0 ,再執行加法操作。

localhost:6379> incrbyfloat test1992 10.2
"2023.2"

Decr 命令

將 key 中儲存的數字值減一。
如果 key 不存在,那麼 key 的值會先被初始化為 0 ,然後再執行 DECR 操作。
如果值包含錯誤的型別,或字串型別的值不能表示為數字,那麼返回一個錯誤。
本操作的值限制在 64 位(bit)有符號數字表示之內。

Decrby 命令

將 key 所儲存的值減去指定的減量值。
如果 key 不存在,那麼 key 的值會先被初始化為 0 ,然後再執行 DECRBY 操作。
如果值包含錯誤的型別,或字串型別的值不能表示為數字,那麼返回一個錯誤。
本操作的值限制在 64 位(bit)有符號數字表示之內。

Append 命令

用於為指定的 key 追加值。
如果 key 已經存在並且是一個字串, APPEND 命令將 value 追加到 key 原來的值的末尾。
如果 key 不存在, APPEND 就簡單地將給定 key 設為 value ,就像執行 SET key value 一樣。

localhost:6379> append myKey 123
(integer) 17
localhost:6379> get myKey
"hollo redis123123"

spring呼叫redis

pom.xml配置 核心依賴

		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.9.0</version>
		</dependency>
		<!-- slf4j日誌統一管理 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.25</version>
		</dependency>
		<!-- spring中的redis -->
		<dependency>
		    <groupId>org.springframework.data</groupId>
		    <artifactId>spring-data-redis</artifactId>
		    <version>1.8.4.RELEASE</version>
		</dependency>

string-redis.xml 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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd   
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-4.3.xsd"
    default-lazy-init="false">
    
    <!-- 佔位符配置檔案 -->  
    <!-- <context:property-placeholder location="classpath:redis.properties" />   -->
    
    <!-- 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>

    <!-- Spring-redis連線池工廠配置 -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="127.0.0.1" />
        <property name="port" value="6379" />
        <property name="timeout" value="2000" />
        <property name="poolConfig" ref="poolConfig" />
    </bean>
    
	<!-- 序列化 String型別 -->
	<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
	<!-- 序列化 物件 -->
	<!-- <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> -->

    <!-- redisTemplate 定義 -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <property name="keySerializer" ref="stringRedisSerializer" />
        <property name="valueSerializer" ref="stringRedisSerializer" />
    </bean>
    
</beans>

測試程式碼1

package redis.string;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

public class RedisStringTest {
	
	private static final Logger logger = LoggerFactory.getLogger(RedisStringTest.class);

	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("redis-conf/string/string-redis.xml");
		RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
		ValueOperations opsForValue = redisTemplate.opsForValue();
		
		// 1.設定值
		opsForValue.set("name", "張三");
		// 2.獲取值
		String name = (String) opsForValue.get("name");
		logger.debug("從redis中獲取name值:{}", name);
		// 3.判斷redis是否含有這個屬性
		Boolean hasKey = redisTemplate.hasKey("name");
		logger.debug("從redis中是否有name屬性:{}", hasKey);
		// 4.刪除redis這個屬性
		redisTemplate.delete("name");
		hasKey = redisTemplate.hasKey("name");
		logger.debug("從redis中是否有name屬性:{}", hasKey);
		// 5.獲取長度
		opsForValue.set("name", "abcdefg");
		Long length = opsForValue.size("name");
		logger.debug("name屬性值長度:{}", length);
		// 6.設定新值,返回舊值
		name = (String) opsForValue.getAndSet("name", "hijklmn");
		logger.debug("從redis中獲取name值:{}", name);
		name = (String) opsForValue.get("name");
		logger.debug("從redis中獲取name值:{}", name);
		// 7.求子串
		String subName = (String) opsForValue.get("name", 1, 4);
		logger.debug("從redis中獲取name子串值:{}", subName);
		// 8.追加
		opsForValue.append("name", "opqrst");
		name = (String) opsForValue.get("name");
		logger.debug("從redis中獲取name值:{}", name);
	}

}

測試程式碼2

package redis.string;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

public class RedisStringTestAppender {
	
	private static final Logger logger = LoggerFactory.getLogger(RedisStringTestAppender.class);

	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("redis-conf/string/string-redis.xml");
		RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
		ValueOperations opsForValue = redisTemplate.opsForValue();
		
		// 1.設定值
		opsForValue.set("i", "3");
		// 2.獲取值
		String name = (String) opsForValue.get("i");
		logger.debug("從redis中獲取i值:{}", name);
		// 3.+1設定
		opsForValue.increment("i", 1);
		name = (String) opsForValue.get("i");
		logger.debug("從redis中獲取i值:{}", name);
		// 4.-1設定
		opsForValue.increment("i", -1);
		name = (String) opsForValue.get("i");
		logger.debug("從redis中獲取i值:{}", name);
		// 5.-1設定
		redisTemplate.getConnectionFactory().getConnection().decr(redisTemplate.getKeySerializer().serialize("i"));
		name = (String) opsForValue.get("i");
		logger.debug("從redis中獲取i值:{}", name);
		// 6.-2設定
		redisTemplate.getConnectionFactory().getConnection().decrBy(redisTemplate.getKeySerializer().serialize("i"), 2);
		name = (String) opsForValue.get("i");
		logger.debug("從redis中獲取i值:{}", name);
	}

}