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"));
}
解析:
- redisTemplate.getConnectionFactory().getConnection()获取的连接是spring data redis 下封装的RedisConnection
- 要想获取原始的连接对象-Jedis对象,需要如下
Jedis jedis = (Jedis) redisTemplate.getConnectionFactory().getConnection().getNativeConnection();
jedis.incrByFloat("key1",0.9);
System.out.println("获取原始redis连接:" + redisTemplate.opsForValue().get("key1"));