栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBoot系列——Redis

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot系列——Redis

前言

  Redis是一个缓存、消息代理和功能丰富的键值存储。StringBoot提供了基本的自动配置。本文记录一下springboot与redis的简单整合实例

  官方文档:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-redis

  

  前期准备

  首先我们要有一个Redis服务,由于我没有Linux环境,为了方便搭建项目,直接在Windows下安装,参考这篇博客:Windows下安装Redis服务

  安装步骤:一直点下一步(偷懒,步骤9、10设置密码我没有设置)

  下载、安装、启动好Redis服务后我们设置一个key并获取一下

  

 

  代码编写

  maven引包

        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        

  配置文件

  我们先看一下都有哪些Redis相关配置,发现好多都有默认值,而且刚好符合我们现在的测试环境,于是乎我的配置文件是这样滴....

server.port=1113spring.application.name=redis-server

  接口测试代码

@RestControllerpublic class Controller{

    @Autowired    private StringRedisTemplate template;

    @RequestMapping("/redis/get/{key}")    private String get(@PathVariable("key") String key){        return template.opsForValue().get(key);
    }

    @RequestMapping("/redis/set/{key}/{value}")    private Boolean set(@PathVariable("key") String key,@PathVariable("value") String value){        boolean flag = true;        try {
            template.opsForValue().set(key,value);
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }        return flag;
    }

}

 

  测试效果

  我们直接启动springboot的main函数,浏览器访问测试接口

 

 

  扩展工具类

  关于StringRedisTemplate类的操作自行查阅资料,我在网上找了一个工具类,我没有测试过,但可以参考自行测试!

@SuppressWarnings("ALL")
@Componentpublic class RedisUtil {    private static StringRedisTemplate template;    
    public RedisUtil(StringRedisTemplate template) {
        RedisUtil.template = template;
    }    
    private void expire(String key, long time) {        try {            if (time > 0) {
                template.expire(key, time, TimeUnit.SECONDS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }    
    public long getExpire(String key) {        return template.getExpire(key, TimeUnit.SECONDS);
    }    
    public boolean hasKey(String key) {        try {            return template.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();            return false;
        }
    }    
    public void del(String... key) {        if (key != null && key.length > 0) {            if (key.length == 1) {
                template.delete(key[0]);
            } else {
                template.delete(CollectionUtils.arrayToList(key));
            }
        }
    }    //============================String=============================  

    
    public Object get(String key) {        return key == null ? null : template.opsForValue().get(key);
    }    
    public boolean set(String key, Object value) {        try {
            template.opsForValue().set(key, String.valueOf(value));            return true;
        } catch (Exception e) {
            e.printStackTrace();            return false;
        }

    }    
    public boolean set(String key, Object value, long time) {        try {            if (time > 0) {
                template.opsForValue().set(key, String.valueOf(value), time, TimeUnit.SECONDS);
            } else {
                set(key, value);
            }            return true;
        } catch (Exception e) {
            e.printStackTrace();            return false;
        }
    }    
    public long incr(String key, long delta) {        if (delta < 0) {            throw new RuntimeException("递增因子必须大于0");
        }        return template.opsForValue().increment(key, delta);
    }    
    public long decr(String key, long delta) {        if (delta < 0) {            throw new RuntimeException("递减因子必须大于0");
        }        return template.opsForValue().increment(key, -delta);
    }    //================================Map=================================  

    
    public Object hget(String key, String item) {        return template.opsForHash().get(key, item);
    }    
    public Map hmget(String key) {        return template.opsForHash().entries(key);
    }    
    public boolean hmset(String key, Map map) {        try {
            template.opsForHash().putAll(key, map);            return true;
        } catch (Exception e) {
            e.printStackTrace();            return false;
        }
    }    
    public boolean hmset(String key, Map map, long time) {        try {
            template.opsForHash().putAll(key, map);            if (time > 0) {
                expire(key, time);
            }            return true;
        } catch (Exception e) {
            e.printStackTrace();            return false;
        }
    }    
    public boolean hset(String key, String item, Object value) {        try {
            template.opsForHash().put(key, item, value);            return true;
        } catch (Exception e) {
            e.printStackTrace();            return false;
        }
    }    
    public boolean hset(String key, String item, Object value, long time) {        try {
            template.opsForHash().put(key, item, value);            if (time > 0) {
                expire(key, time);
            }            return true;
        } catch (Exception e) {
            e.printStackTrace();            return false;
        }
    }    
    public void hdel(String key, Object... item) {
        template.opsForHash().delete(key, item);
    }    
    public boolean hHasKey(String key, String item) {        return template.opsForHash().hasKey(key, item);
    }    
    public double hincr(String key, String item, double by) {        return template.opsForHash().increment(key, item, by);
    }    
    public double hdecr(String key, String item, double by) {        return template.opsForHash().increment(key, item, -by);
    }    //============================Set=============================

    
    public Set sGet(String key) {        try {            return Collections.singleton(template.opsForSet().members(key));
        } catch (Exception e) {
            e.printStackTrace();            return null;
        }
    }    
    public boolean sHasKey(String key, Object value) {        try {            return template.opsForSet().isMember(key, value);
        } catch (Exception e) {
            e.printStackTrace();            return false;
        }
    }    
    public long sSet(String key, Object... values) {        try {            return template.opsForSet().add(key, String.valueOf(values));
        } catch (Exception e) {
            e.printStackTrace();            return 0;
        }
    }    
    public long sSetAndTime(String key, long time, String... values) {        try {
            Long count = template.opsForSet().add(key, values);            if (time > 0) {
                expire(key, time);
            }            return count;
        } catch (Exception e) {
            e.printStackTrace();            return 0;
        }
    }    
    public long sGetSetSize(String key) {        try {            return template.opsForSet().size(key);
        } catch (Exception e) {
            e.printStackTrace();            return 0;
        }
    }    
    public long setRemove(String key, Object... values) {        try {
            Long count = template.opsForSet().remove(key, values);            return count;
        } catch (Exception e) {
            e.printStackTrace();            return 0;
        }
    }    //===============================list=================================  

    
    public List lGet(String key, long start, long end) {        try {            return template.opsForList().range(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();            return null;
        }
    }    
    public long lGetListSize(String key) {        try {            return template.opsForList().size(key);
        } catch (Exception e) {
            e.printStackTrace();            return 0;
        }
    }    
    public Object lGetIndex(String key, long index) {        try {            return template.opsForList().index(key, index);
        } catch (Exception e) {
            e.printStackTrace();            return null;
        }
    }    
    public boolean lSet(String key, Object value) {        try {
            template.opsForList().rightPush(key, (String) value);            return true;
        } catch (Exception e) {
            e.printStackTrace();            return false;
        }
    }    
    public boolean lSet(String key, Object value, long time) {        try {
            template.opsForList().rightPush(key, (String) value);            if (time > 0) {
                expire(key, time);
            }            return true;
        } catch (Exception e) {
            e.printStackTrace();            return false;
        }
    }    
    public boolean lSet(String key, List value) {        try {
            template.opsForList().rightPushAll(key, String.valueOf(value));            return true;
        } catch (Exception e) {
            e.printStackTrace();            return false;
        }
    }    
    public boolean lSet(String key, List value, long time) {        try {
            template.opsForList().rightPushAll(key, String.valueOf(value));            if (time > 0) {
                expire(key, time);
            }            return true;
        } catch (Exception e) {
            e.printStackTrace();            return false;
        }
    }    
    public boolean lUpdateIndex(String key, long index, Object value) {        try {
            template.opsForList().set(key, index, (String) value);            return true;
        } catch (Exception e) {
            e.printStackTrace();            return false;
        }
    }    
    public long lRemove(String key, long count, Object value) {        try {
            Long remove = template.opsForList().remove(key, count, value);            return remove;
        } catch (Exception e) {
            e.printStackTrace();            return 0;
        }
    }
}

View Code

 

   后记

  这只是一个单机版的Redis服务,而且还是Windows上面的,后续有空再搭建Redis集群

作者:huanzi-qch

出处:https://www.cnblogs.com/huanzi-qch/p/10239888.html  

若标题中有“转载”字样,则本文版权归原作者所有。若无转载字样,本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.


转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号