• java redis api及test demo


    1.CacheService.java

    package com.redis.demo;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Component;
    import org.springframework.util.StringUtils;
    
    import java.util.Collection;
    import java.util.Collections;
    import java.util.Date;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    
    @Component
    public class CacheService {
        private static Logger log = LoggerFactory.getLogger(CacheService.class);
    
        @Autowired
        private StringRedisTemplate redisTemplate;
    
        private final String DEFAULT_KEY_PREFIX = "redisTest";
        private final int EXPIRE_TIME = 1;
        private final TimeUnit EXPIRE_TIME_TYPE = TimeUnit.DAYS;
    
    
        /**
         * 数据缓存至redis
         *
         * @param key
         * @param value
         * @return
         */
        public <K, V> void add(K key, V value) {
            try {
                if (value != null) {
                    redisTemplate
                            .opsForValue()
                            .set(DEFAULT_KEY_PREFIX + key, JSON.toJSONString(value));
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                throw new RuntimeException("数据缓存至redis失败");
            }
        }
    
        /**
         * 数据缓存至redis并设置过期时间
         *
         * @param key
         * @param value
         * @return
         */
        public <K, V> void add(K key, V value, long timeout, TimeUnit unit) {
            try {
                if (value != null) {
                    redisTemplate
                            .opsForValue()
                            .set(DEFAULT_KEY_PREFIX + key, JSON.toJSONString(value), timeout, unit);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                throw new RuntimeException("数据缓存至redis失败");
            }
        }
    
        /**
         * 写入 hash-set,已经是key-value的键值,不能再写入为hash-set
         *
         * @param key    must not be {@literal null}.
         * @param subKey must not be {@literal null}.
         * @param value  写入的值
         */
        public <K, SK, V> void addHashCache(K key, SK subKey, V value) {
            redisTemplate.opsForHash().put(DEFAULT_KEY_PREFIX + key, subKey, value);
        }
    
        /**
         * 写入 hash-set,并设置过期时间
         *
         * @param key    must not be {@literal null}.
         * @param subKey must not be {@literal null}.
         * @param value  写入的值
         */
        public <K, SK, V> void addHashCache(K key, SK subKey, V value, long timeout, TimeUnit unit) {
            redisTemplate.opsForHash().put(DEFAULT_KEY_PREFIX + key, subKey, value);
            redisTemplate.expire(DEFAULT_KEY_PREFIX + key, timeout, unit);
        }
    
        /**
         * 获取 hash-setvalue
         *
         * @param key    must not be {@literal null}.
         * @param subKey must not be {@literal null}.
         */
        public <K, SK> Object getHashCache(K key, SK subKey) {
            return  redisTemplate.opsForHash().get(DEFAULT_KEY_PREFIX + key, subKey);
        }
    
    
        /**
         * 从redis中获取缓存数据,转成对象
         *
         * @param key   must not be {@literal null}.
         * @param clazz 对象类型
         * @return
         */
        public <K, V> V getObject(K key, Class<V> clazz) {
            String value = this.get(key);
            V result = null;
            if (!StringUtils.isEmpty(value)) {
                result = JSONObject.parseObject(value, clazz);
            }
            return result;
        }
    
        /**
         * 从redis中获取缓存数据,转成list
         *
         * @param key   must not be {@literal null}.
         * @param clazz 对象类型
         * @return
         */
        public <K, V> List<V> getList(K key, Class<V> clazz) {
            String value = this.get(key);
            List<V> result = Collections.emptyList();
            if (!StringUtils.isEmpty(value)) {
                result = JSONArray.parseArray(value, clazz);
            }
            return result;
        }
    
        /**
         * 功能描述:Get the value of {@code key}.
         *
         * @param key must not be {@literal null}.
         * @return java.lang.String
         * @date 2021/9/19
         **/
        public <K> String get(K key) {
            String value;
            try {
                value = redisTemplate.opsForValue().get(DEFAULT_KEY_PREFIX + key);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                throw new RuntimeException("从redis缓存中获取缓存数据失败");
            }
            return value;
        }
    
        /**
         * 删除key
         */
        public void delete(String key) {
            redisTemplate.delete(key);
        }
    
        /**
         * 批量删除key
         */
        public void delete(Collection<String> keys) {
            redisTemplate.delete(keys);
        }
    
        /**
         * 序列化key
         */
        public byte[] dump(String key) {
            return redisTemplate.dump(key);
        }
    
        /**
         * 是否存在key
         */
        public Boolean hasKey(String key) {
            return redisTemplate.hasKey(key);
        }
    
        /**
         * 设置过期时间
         */
        public Boolean expire(String key, long timeout, TimeUnit unit) {
            return redisTemplate.expire(key, timeout, unit);
        }
    
        /**
         * 设置过期时间
         */
        public Boolean expireAt(String key, Date date) {
            return redisTemplate.expireAt(key, date);
        }
    
    
        /**
         * 移除 key 的过期时间,key 将持久保持
         */
        public Boolean persist(String key) {
            return redisTemplate.persist(key);
        }
    
        /**
         * 返回 key 的剩余的过期时间
         */
        public Long getExpire(String key, TimeUnit unit) {
            return redisTemplate.getExpire(key, unit);
        }
    
        /**
         * 返回 key 的剩余的过期时间
         */
        public Long getExpire(String key) {
            return redisTemplate.getExpire(key);
        }
    
    }

    2.CacheServiceTest.java

    /**
     * 
     */
    package com.redis.demo;
    
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes=DemoApplication.class)
    public class BaseTest {
    
    }
    package com.redis.demo;
    
    import com.alibaba.fastjson.JSON;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.ArrayList;
    import java.util.List;
    
    //@SpringBootTest
    class DemoApplicationTests extends BaseTest{
        @Autowired
        CacheService cacheService;
    
        @Test
        void contextLoads() {
            //添加string
            cacheService.add("orderNumber",100);
            String orderNumberValue = cacheService.get("orderNumber");
            System.out.println("orderNumberValue=" + orderNumberValue);
    
            cacheService.addHashCache("loginmember","username","zhangsanfeng");
            cacheService.addHashCache("loginmember","password","123456");
    
            System.out.println("username="+cacheService.getHashCache("loginmember","username"));
            System.out.println("password="+cacheService.getHashCache("loginmember","password"));
    
            UserVo vo = new UserVo();
            vo.setPassword("111111");
            vo.setUsername("admin");
            cacheService.add("userVo", vo);
            System.out.println("object-username="+cacheService.getObject("userVo",UserVo.class).getUsername());
            System.out.println("object-password="+cacheService.getObject("userVo",UserVo.class).getPassword());
    
            List list = new ArrayList();
            list.add(vo);
            cacheService.add("userVoList", list);
            List<UserVo> getList = cacheService.getList("userVoList",UserVo.class);
            for (UserVo v:getList) {
                System.out.println("对象="+JSON.toJSONString(v));
            }
    
        
         //key需要带上前缀,否则key不对 System.out.println("是否存在key=" + cacheService.hasKey("userVoList")); System.out.println("剩余的过期时间=" + cacheService.getExpire("userVoList")); //删除key cacheService.delete("userVoList"); } }

    3.测试运行结果

    orderNumberValue=100
    username=zhangsanfeng
    password=123456
    object-username=admin
    object-password=111111
    对象={"password":"111111","username":"admin"}
    是否存在key=false
    剩余的过期时间=-2

  • 相关阅读:
    爬虫之字体解密
    【大数据】PySpark 使用 FileSystem 操作 HDFS
    数据读取保存(五)
    Spark SQL 数据源(三)
    hive 之 DML 操作(六)
    【Python】散列的一种实现
    【Python】插入记录并获取其自增ID(PyMysql)
    sbt 编译打包(六)
    Spark SQL (二)
    2022年的第一篇
  • 原文地址:https://www.cnblogs.com/oktokeep/p/16341576.html
Copyright © 2020-2023  润新知