了解Redis数据结构——字符串

字符串是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属性,对键进行了序列化,如果获取结果也可以进行同样的转换。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值