美文网首页
java_redis快速使用

java_redis快速使用

作者: 七枷琴子 | 来源:发表于2019-12-10 18:01 被阅读0次

新建一个枚举用来管理redis的过期时间

package com.shareworx.apm.datacenter.redisCache;

import java.util.concurrent.TimeUnit;

/**
 * redis缓存时间控制枚举
 */


public enum CacheKeyPrefix {

    TestRedis("test_redis", "测试redis使用", TimeUnit.SECONDS.toSeconds(10))
    ;

    private CacheKeyPrefix(String key, String desc) {
        this.key = key;
        this.desc = desc;
    }

    private CacheKeyPrefix(String value, String desc, long timeout) {
        this.key = value;
        this.desc = desc;
        this.timeout = timeout;
    }

    private String key;
    private String desc;
    private long timeout;


    public String getKey() {
        return key;
    }

    public String getDesc() {
        return desc;
    }

    public long getTimeout() {
        return timeout;
    }

}

注入服务

package com.shareworx.apm.datacenter.redisCache;

import org.assertj.core.util.Lists;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "/redis")
public class RedisCacheAction {

    @Resource(name = "redisTemplate")
    private RedisTemplate<String, List<String>> cacheTest;


    @GetMapping(value = "/test1/{cacheKey}")
    public String test1(@PathVariable String cacheKey) {
        cacheTest.opsForValue().set(cacheKey, Lists.newArrayList("111223", "中文中文"), CacheKeyPrefix.TestRedis.getTimeout(), TimeUnit.SECONDS);
        return "缓存成功,key:" + cacheKey;
    }

    @GetMapping(value = "/test2/{cacheKey}")
    public String test2(@PathVariable String cacheKey) {
        String str = "";
        if (!cacheTest.hasKey(cacheKey)) {
            str = "null";
            return str;
        }
        List<String> result = cacheTest.opsForValue().get(cacheKey);
        for (String s : result) {
            System.out.println(s);
            str = str + " " + s;
        }
        return str;
    }
}

但是因为序列化方式是jdk的,所以用Redisdeskmanager等软件连接,看到的是乱码,故而要进行序列化后操作

package com.shareworx.apm.datacenter.redisCache;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

/**
 * Redis缓存配置类
 *
 */
@Configuration
@EnableCaching
public class RedisConfigurer extends CachingConfigurerSupport {

    /**
     * 注意设置过期时间
     */
//    @Bean
//    public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) {
//        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
//        Map<String, Long> expires = new HashMap<>();
//        expires.put("access_token_cache", 1800L);//过期时间秒为单位,默认为30分钟
//        expires.put("nonce_cache", 600L);//默认为10分钟
//        expires.put("allotting_autocancel", 300L);//默认五分钟
//        cacheManager.setExpires(expires);
//        return cacheManager;
//    }
    @Bean(name = "apmRedisTemplate")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
//        StringRedisTemplate template = new StringRedisTemplate(factory);
////        template.setConnectionFactory(factory);
//        //key序列化
//        RedisSerializer<String> redisSerializer = new StringRedisSerializer();

        //value序列化,value hashmap序列化
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(jackson2JsonRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();

        return template;
    }

}

相关文章

  • java_redis快速使用

    新建一个枚举用来管理redis的过期时间 注入服务 但是因为序列化方式是jdk的,所以用Redisdeskmana...

  • Git 快速使用

    MENU Git 快速使用 之 配置并获取SSH公钥Git 快速使用 之 Git 分支 branchGit 快速使...

  • Masonry介绍与使用实践:快速上手Autolayout

    Masonry介绍与使用实践:快速上手Autolayout Masonry介绍与使用实践:快速上手Autolayout

  • 创建election项目的几种方式

    一:手动创建 二:克隆官方的快速启动项目 三:使用electron-forge快速生成 四:使用npx快速创建

  • node.js常见命令

    1、使用↑键,可以快速定位到上一次执行的命令2、使用tab 键,能够快速补全路径3、使用esc 键,能够快速清空当...

  • Airtest开发App爬虫

    使用Airtest超快速开发App爬虫 在Airtest中如何正确使用无线模式控制手机在使用Airtest超快速开...

  • Fiddler的使用(一)

    #Fiddler的使用一:快速配置和使用 快速使用主要是完成 Fiddler的设置 和 手机端的设置(浏览器的设置...

  • 循环对象

    使用Object.values快速获取对象键值 使用Object.keys快速获取对象键名 解构时设置默认值

  • Docker基础以及实践

    我使用docker,也只是简单的使用,主要用来快速搭建环境,比如快速搭建Elasticsearch环境,进行各种验...

  • Egg

    项目初始化 使用GIT创建项目,本地克隆项目并进入。 使用脚手架快速初始化,使用npm init egg快速选择适...

网友评论

      本文标题:java_redis快速使用

      本文链接:https://www.haomeiwen.com/subject/noiggctx.html