Redis缓存

来源:互联网 发布:原生js模拟select 编辑:程序博客网 时间:2024/06/01 17:53
redis作为key-value型的存储系统,尤其是其支持的value类型更多,使用者可以选择redis作数据库,也可作为缓存实现。这里,简单的介绍下使用redis作为缓存实现(以RedisTemplate操作字符串为例)

1、pom.xml中添加依赖
<!-- redis -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.1.1.RELEASE</version>
    <exclusions>
        <exclusion>
            <artifactId>jcl-over-slf4j</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.1.0</version>
</dependency>

2、编写redis配置文件 spring-redis.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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd"
       default-lazy-init="true">
    <description>redis配置</description>

    <!-- Jedis 连接池配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxActive" value="1000"/>
        <property name="maxIdle" value="100"/>
        <property name="maxWait" value="90000"/>
        <property name="testOnBorrow" value="true"/>
    </bean>
    <!-- Jedis ConnectionFactory 数据库连接配置 -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="masterredis.simonsfan.cn"/>
        <property name="port" value="6426"/>
        <property name="password" value=""/>
        <property name="poolConfig" ref="jedisPoolConfig"/>
    </bean>
    <!-- jedisTemplate 配置 -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"></property>
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
    </bean>
</beans>

3、导入spring主配置文件
<import resource="classpath:spring-redis.xml" />

4、编写测试类
@Controller
public class UserController {
    @Autowired
    private UserActivityInfoMapper userActivityInfoMapper;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @ResponseBody
    @RequestMapping(value = "/activity", produces = "application/json;charset=utf-8")
    public String selectByPrimaryKey(@RequestParam(required = false) int id) {
        String value = null;
        String redisKey = "test_redis_key";
        value = redisTemplate.opsForValue().get(redisKey);
        if (value != null) {
            return value;
        }
        value = userActivityInfoMapper.selectByPrimaryKey(id).getActName();
        if (value != null) {
            redisTemplate.opsForValue().set(redisKey, value);
        }
        return value;
    }
}

有时情况下需要指定缓存时间

redisTemplate.opsForValue().set(redisKey, value, 1800, TimeUnit.SECONDS)或

redisTemplate.expire(redisKey, 1800 , TimeUnit.SECONDS)

表示缓存有效期为1800s

5、测试浏览器多次访问该接口,端点调试,发现第二次访问开始,value值是从redis缓存中获取的

也可以来通过如下方法设置缓存时间
redisTemplate.opsForValue().set(redisKey, value, 1800, TimeUnit.SECONDS)

操作value为字符串类型的常用方法



6、RedisTemplate用来操作不同value类型的方法:

redisTemplate.opsForValue();   //操作字符串
redisTemplate.opsForHash();    //操作hash
redisTemplate.opsForList();      //操作list
redisTemplate.opsForSet();      //操作set
redisTemplate.opsForZSet();    //操作有序set0
原创粉丝点击