Redis字符串

Redis字符串

字符串是Redis最基本的数据结构,它将一个键和一个值存储于Redis内部,犹如Java的Map结构,让Redis通过键去找到值

首先配置Spring关于Redis字符串的运行环境

    <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="stringRedisSerializer" />
    </bean> 

这里给RedisTemplate的键值序列化器设置为String类型,所以它就是一种字符串操作

做如下的测试:

        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); //value1

        //通过key删除值
        redisTemplate.delete("key1");
        value1 = (String)redisTemplate.opsForValue().get("key1");
        System.out.println(value1); //null

        //求长度
        Long length = redisTemplate.opsForValue().size("key2");
        System.out.println(length); //6

        //设置新值并返回旧值
        String oldValue2 = (String)redisTemplate.opsForValue().getAndSet("key2", "new_value2");
        System.out.println(oldValue2); //value2

        //通过key获取值
        String value2 = (String)redisTemplate.opsForValue().get("key2");
        System.out.println(value2); //new_value2

        //求子串
        String rangeValue2 = redisTemplate.opsForValue().get("key2", 0, 3);
        System.out.println(rangeValue2); //new_

        //追加字符串到末尾,返回新串长度
        int newLen = redisTemplate.opsForValue().append("key2", "_app");
        System.out.println(newLen); //14

        String appendValue2 = (String)redisTemplate.opsForValue().get("key2");
        System.out.println(appendValue2); //new_value2_app

在Spring中,redisTemplate.opsForValue()所返回的对象可以操作简单的键值对,可以是字符串,也可以是对象,具体依据你所配置的序列化方案

如果字符串是数字,Redis还能支持简单的运算

    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);

        redisTemplate.opsForValue().set("i", "9");
        printCurrValue(redisTemplate, "i"); //9

        //整数加法
        redisTemplate.opsForValue().increment("i", 1);
        printCurrValue(redisTemplate, "i"); //10
        //整数减法
        redisTemplate.getConnectionFactory().getConnection().decr(redisTemplate.getKeySerializer().serialize("i"));
        printCurrValue(redisTemplate, "i"); //9
        redisTemplate.getConnectionFactory().getConnection().decrBy(redisTemplate.getKeySerializer().serialize("i"), 6);
        printCurrValue(redisTemplate, "i"); //3

        //浮点数
        redisTemplate.opsForValue().increment("i", 2.3);
        printCurrValue(redisTemplate, "i"); //5.3

    }

    public static void printCurrValue(RedisTemplate redisTemplate, String key) {
        String i = (String)redisTemplate.opsForValue().get(key);
        System.out.println(i);
    }

减法,原有值必须是整数,否则就会引发异常

猜你喜欢

转载自blog.csdn.net/winfredzen/article/details/80340211