redis 字符串(String) (redis学习三)

版权声明:随意转载。 https://blog.csdn.net/dengjili/article/details/84893044

基本命令

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);
	}

}

猜你喜欢

转载自blog.csdn.net/dengjili/article/details/84893044