2.Redis的数据结构

Redis的六种数据结构

2.1 字符串

字符串是redis最基本的数据结构,它是将以一个键和一个值存储于Redis内部.

2.1.1 Redis客户端命令:

2.1.2在spring中使用以上命令

Spring的redisTemplate类(以及opsForValue方法)中封装了一定的redis的客户端命令

 比如:

redisTemplate.opsForValue().set("key1","value1");

等价于:

 

 redisTemplate.getConnectionFactory().getConnection().set(redisTemplate.getKeySerializer().serialize("key1"),"value1".getBytes());

 

实例:

1.spring-mvc-redis.xml

  <mvc:annotation-driven/>

    <context:component-scan base-package="Dao,Tenseven"/>

    <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="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

        <property name="hostName" value="localhost"/>

        <property name="port" value="6379"/>

        <property name="poolConfig" ref="poolConfig"/>

    </bean>

    <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>



    <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>

2.运行类

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = "classpath:spring-mvc-redis.xml")

public class test1 {

    @Autowired

    private RedisTemplate redisTemplate;

@Test

    public void test3() {

        redisTemplate.opsForValue().set("key1","value1");

        System.out.println("修改前" + redisTemplate.opsForValue().get("key1"));



        redisTemplate.opsForValue().getAndSet("key1","v");

        System.out.println("修改后" + redisTemplate.opsForValue().get("key1"));



        Long size = redisTemplate.opsForValue().size("key1");

        System.out.println("size:"+size);



        Integer append = redisTemplate.opsForValue().append("key1","abc");

        System.out.println("append 后:"+redisTemplate.opsForValue().get("key1"));



        System.out.println("截取0-1:"+redisTemplate.opsForValue().get("key1",0,1));



        redisTemplate.delete("key1");

        System.out.println("删除后:"+redisTemplate.opsForValue().get("key1"));



    }



}

结果:

修改前value1

修改后v

size:1

append 后:vabc

截取0-1:va

删除后:null

2.1.3 Redis支持简单的对整型/浮点的操作

 

2.1.3.1客户端命令:

 

2.1.3.2 Spring使用

有的redisTemplate没有封装,所以要自己获取连接执行了.

  public void test4() {

        redisTemplate.opsForValue().set("key1","1");

        System.out.println("修改前" + redisTemplate.opsForValue().get("key1"));



        redisTemplate.opsForValue().increment("key1",1);

        System.out.println("增加1:" + redisTemplate.opsForValue().get("key1"));



        redisTemplate.opsForValue().increment("key1",9);

        System.out.println("增加9:" + redisTemplate.opsForValue().get("key1"));



        redisTemplate.getConnectionFactory().getConnection().decr(redisTemplate.getKeySerializer().serialize("key1"));

        System.out.println("减掉1:" + redisTemplate.opsForValue().get("key1"));



        redisTemplate.getConnectionFactory().getConnection().decrBy(redisTemplate.getKeySerializer().serialize("key1"),3);

        System.out.println("减掉3:" + redisTemplate.opsForValue().get("key1"));



        redisTemplate.opsForValue().increment("key1",1.2);

        System.out.println("增加float-1.2:" + redisTemplate.opsForValue().get("key1"));

    }

 

解析:

 

  1. redisTemplate.getConnectionFactory().getConnection()获取的连接是spring data redis 下封装的RedisConnection
  2. 要想获取原始的连接对象-Jedis对象,需要如下
Jedis jedis = (Jedis) redisTemplate.getConnectionFactory().getConnection().getNativeConnection();
jedis.incrByFloat("key1",0.9);
System.out.println("获取原始redis连接:" + redisTemplate.opsForValue().get("key1"));

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值