Spring 整合Redis以及Spring的RedisTemplate如何使用

13 篇文章 1 订阅

1.添加jar包

  <dependency>
     <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
  </dependency>
  <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.8.3.RELEASE</version>
  </dependency>

2.配置文件

<!--资源池-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="1"/>
        <property name="maxTotal" value="5"/>
        <property name="blockWhenExhausted" value="true"/>
        <property name="maxWaitMillis" value="30000"/>
        <property name="testOnBorrow" value="true"/>
    </bean>

    <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="poolConfig" ref="jedisPoolConfig"/>
        <property name="usePool" value="true"/>
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
    </bean>

3.测试

package token;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

import java.util.*;

import static org.junit.Assert.*;

/**
 * RedisTemplateTest 
 * Created by heqianqian on 2017/5/9.
 */
@ContextConfiguration(locations = "classpath:spring-redis.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class RedisTemplateTest {

    @Resource
    private RedisTemplate redisTemplate;

    @Test
    public void testString() throws Exception {
        //添加一个key
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("csdn", "river");
        //获取一个key
        Assert.assertEquals("river", valueOperations.get("csdn"));
    }

    @Test
    public void testMap() throws Exception {
        //添加一个hash集合
        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
        //hashOperations.put("article","java","java-io");
        //hashOperations.put("article","mysql","procedure");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("java", "java-io");
        map.put("mysql", "procedure");
        hashOperations.putAll("article", map);
        //获取map
        Map<Object, Object> article = hashOperations.entries("article");
        for (Map.Entry<Object, Object> entry : article.entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }

    @Test
    public void testList() throws Exception {
        ListOperations listOperations = redisTemplate.opsForList();
        listOperations.rightPush("queue", "i'm right");
        listOperations.rightPush("queue", "i'm right2");
        listOperations.leftPush("queue", "i'm left");
        listOperations.leftPush("queue", "i'm left2");

        List queue = listOperations.range("queue", 0, 4);
        System.out.println(queue);

        //[i'm left2, i'm left, i'm right, i'm right2]
    }

    @Test
    public void testSet() throws Exception {
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add("set", "a");
        setOperations.add("set", "b");
        setOperations.add("set", "c");
        setOperations.add("set", "c");
        System.out.println(setOperations.members("set"));
    }

    @Test
    public void testOrderedSet() throws Exception {
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.add("zset", "a", 0);
        zSetOperations.add("zset", "b", 2);
        zSetOperations.add("zset", "c", 1);
        System.out.println(zSetOperations.rangeByScore("zset", 0, 2));
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值