zoukankan      html  css  js  c++  java
  • 【一篇文章就够了】Redis入门

    什么是NoSQL?

    NoSQL: not only sql,不仅仅是sql

    NoSQL的种类

    NosQL数据库的四大分类如下:

    • 键值(Key-Value)存储数据库

    相关产品:Tokyo Cabinet/Tyrant、Redis、Voldemort、Berkeley DB
    典型应用:内容缓存,主要用于处理大量数据的高访问负载。
    数据模型:一系列键值对
    优势:快速查询
    劣势:存储的数据缺少结构化

    • 列存储数据库

    相关产品:Cassandra、HBase、Riak
    典型应用:分布式的文件系统
    数据模型:以列簇式存储,将同一列数据存在一起
    优势:查找速度快,可扩展性强,更容易进行分布式扩展
    劣势:功能相对局限

    • 文档型数据库

    相关产品: CouchDB、MongoDB
    典型应用:Web应用(与Key-Value类似,Value是结构化的)
    数据模型:一系列键值对
    优势:数据结构要求不严格
    劣势:查询性能不高,而且缺乏统一的查询语法

    • 图形(Graph)数据库

    相关数据库:Neo4j、InfoGrid、Infinite Graph
    典型应用:社交网络
    数据模型:图结构
    优势:利用图结构相关算法。
    劣势:需要对整个图做计算才能得出结果,不容易做分布式的集群方案。

    NoSQL的优缺点

    随着互联网web2.0网站的兴起,非关系型的数据库现在成了一个极其热门的新领域,非关系数据库产品的发展非常迅速。而传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的web2.0纯动态网站已经显得力不从心,暴露了很多难以克服的问题,例如:
    1、对数据库高并发读写的需求
    一些热门的BBS网站,每天需要上万次的数据存储,不断地sql写数据请求,让IO硬盘无法承受。
    2、对海量数据的高效率存储和访问的需求
    微信、QQ用户众多,在亿级别的数据表中查找数据,效率极低。
    3、对数据库的高可扩展性和高可用性的需求

    这些困难就是NoSQL数据库的优点,因为它可以解决这些问题?

    同时NoSQL也存在缺点,例如
    1、增加程序员的学习成本
    2、NoSQL的数据是存储在内存中的,而内存一般都不会很大,无法存储大量数据。所以解决的办法就是SQL和NoSQL结合使用。

    什么是Redis

    Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库。它通过提供多种键值数据类型来适应不同场景下的存储需求。

    Redis的特点

    1.易扩展
    NosQL 数据库种类繁多,但是一个共同的特点都是去掉关系数据库的关系型特性。数据之间无关系,这样就非常容易扩展。也无形之间,在架构的层面上带来了可扩展的能力。
    2.大数据量,高性能
    NoSQL 数据库都具有非常高的读写性能,尤其在大数据量下,同样表现优秀。这得益于它的无关系性,数据库的结构简单。
    3.灵活的数据模型
    NoSQL无需事先为要存储的数据建立字段,随时可以存储自定义的数据格式。而在关系数据库里,增删字段是一件非常麻烦的事情。如果是非常大数据量的表,增加字段简直就是一个噩梦。这点在大数据量的 Web2.0时代尤其明显。
    4.高可用
    NoSQL在不太影响性能的情况,就可以方便的实现高可用的架构。比如Cassandra,HBase模型,通过复制模型也能实现高可用。

    Redis的应用场景

    • 缓存(数据查询、短连接、新闻内容、商品内容等等)。(最多使用)
    • 聊天室的在线好友列表。
    • 任务队列。(秒杀、抢购、12306等等)
    • 应用排行榜。
    • 网站访问统计。

    Redis 指令

    Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。主要学习针对这五种数据类型的操作,同时还有发布订阅、事务等也可以浏览学习。详细内容如下:
    Redis 命令|菜鸟教程
    Redis 客户端redis-cli使用

    各个数据类型应用场景

    Java集成Redis

    不使用框架的话可以使用Jedis,使用Spring框架的话可以使用Spring-data-redis。但是在今天SpringBoot乃框架之集大成者,所以之前的哪些都不学了,主要使用RedisTemplate来实现Java集成Redis。

    第一步:创建一个springboot项目,并导入redis相关依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            <!--fastjson-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.75</version>
            </dependency>
    

    第二步:编写配置文件

    #Redis
    #spring.redis.host=127.0.0.1
    redis.host=127.0.0.1
    ## Redis服务器连接端口
    redis.port=6379
    ## 连接超时时间(毫秒)
    redis.timeout=3
    ## Redis服务器连接密码(默认为空)
    redis.password=
    ## 连接池中的最大连接数
    redis.poolMaxTotal=10
    ## 连接池中的最大空闲连接
    redis.poolMaxIdle=10
    ## 连接池最大阻塞等待时间(使用负值表示没有限制)
    redis.poolMaxWait=3
    

    第三步:编写配置类

    @Configuration
    public class RedisConfig {
    
        @Bean
        public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate<Object, Object> template = new RedisTemplate<>();
            // 使用fastjson序列化
            FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
            // value值的序列化采用fastJsonRedisSerializer
            template.setValueSerializer(fastJsonRedisSerializer);
            template.setHashValueSerializer(fastJsonRedisSerializer);
            // key的序列化采用StringRedisSerializer
            template.setKeySerializer(new StringRedisSerializer());
            template.setHashKeySerializer(new StringRedisSerializer());
    
            template.setConnectionFactory(redisConnectionFactory);
            return template;
        }
    
        @Bean
        public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
            StringRedisTemplate template = new StringRedisTemplate();
            template.setConnectionFactory(redisConnectionFactory);
            return template;
        }
    }
    

    第四步:使用Redis

    @RestController
    @RequestMapping("/test")
    public class TestController {
    
        @Autowired
        RedisTemplate redisTemplate;
    
        @Autowired
        StringRedisTemplate stringRedisTemplate;
    
        @GetMapping("/getUser")
        public String getUser() {
            return stringRedisTemplate.opsForValue().get("user");
        }
    
        @GetMapping("/setUser")
        public String setUser(@RequestParam("id") int id) {
            try {
                stringRedisTemplate.opsForValue().set("user", String.valueOf(id));
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e);
            }
            return "succeed";
        }
    
        @GetMapping("/getUserList")
        public Object getUserList() {
            List<String> user = (List<String>) redisTemplate.opsForList().range("user", 0, 10);
            return user;
        }
    
        @GetMapping("/setUserList")
        public String setUserList(@RequestParam("count") int count,
                                  @RequestParam("name") String name) {
            try {
                List<String> stringList = new ArrayList<>();
                for (int i = 0; i < count; i++) {
                    redisTemplate.opsForList().leftPush("user", name);
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e);
            }
            return "succeed";
        }
    }
    

    使用之前需要开启本地Redis。RedisTemplate如何操作Redis?点击这里查看详情

    RedisUtil

    @Component
    public class RedisUtil {
        @Resource
        private RedisTemplate<String, Object> redisTemplate;
    
        public Set<String> keys(String keys) {
            try {
                return redisTemplate.keys(keys);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
        /**
         * 指定缓存失效时间
         *
         * @param key  键
         * @param time 时间(秒)
         * @return
         */
        public boolean expire(String key, long time) {
            try {
                if (time > 0) {
                    redisTemplate.expire(key, time, TimeUnit.SECONDS);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 根据key 获取过期时间
         *
         * @param key 键 不能为null
         * @return 时间(秒) 返回0代表为永久有效
         */
        public long getExpire(String key) {
            return redisTemplate.getExpire(key, TimeUnit.SECONDS);
        }
    
        /**
         * 判断key是否存在
         *
         * @param key 键
         * @return true 存在 false不存在
         */
        public boolean hasKey(String key) {
            try {
                return redisTemplate.hasKey(key);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 删除缓存
         *
         * @param key 可以传一个值 或多个
         */
        @SuppressWarnings("unchecked")
        public void del(String... key) {
            if (key != null && key.length > 0) {
                if (key.length == 1) {
                    redisTemplate.delete(key[0]);
                } else {
                    redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));
                }
            }
        }
    
        /**
         * 普通缓存获取
         *
         * @param key 键
         * @return 值
         */
        public Object get(String key) {
            return key == null ? null : redisTemplate.opsForValue().get(key);
        }
    
        /**
         * 普通缓存放入
         *
         * @param key   键
         * @param value 值
         * @return true成功 false失败
         */
        public boolean set(String key, Object value) {
            try {
                redisTemplate.opsForValue().set(key, value);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 普通缓存放入并设置时间
         *
         * @param key   键
         * @param value 值
         * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
         * @return true成功 false 失败
         */
        public boolean set(String key, Object value, long time) {
            try {
                if (time > 0) {
                    redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
                } else {
                    set(key, value);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 递增
         *
         * @param key   键
         * @param delta 要增加几(大于0)
         * @return
         */
        public long incr(String key, long delta) {
            if (delta < 0) {
                throw new RuntimeException("递增因子必须大于0");
            }
            return redisTemplate.opsForValue().increment(key, delta);
        }
    
        /**
         * 递减
         *
         * @param key   键
         * @param delta 要减少几(小于0)
         * @return
         */
        public long decr(String key, long delta) {
            if (delta < 0) {
                throw new RuntimeException("递减因子必须大于0");
            }
            return redisTemplate.opsForValue().increment(key, -delta);
        }
    
        /**
         * HashGet
         *
         * @param key  键 不能为null
         * @param item 项 不能为null
         * @return 值
         */
        public Object hget(String key, String item) {
            return redisTemplate.opsForHash().get(key, item);
        }
    
        /**
         * 获取hashKey对应的所有键值
         *
         * @param key 键
         * @return 对应的多个键值
         */
        public Map<Object, Object> hmget(String key) {
            return redisTemplate.opsForHash().entries(key);
        }
    
        /**
         * HashSet
         *
         * @param key 键
         * @param map 对应多个键值
         * @return true 成功 false 失败
         */
        public boolean hmset(String key, Map<String, Object> map) {
            try {
                redisTemplate.opsForHash().putAll(key, map);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * HashSet 并设置时间
         *
         * @param key  键
         * @param map  对应多个键值
         * @param time 时间(秒)
         * @return true成功 false失败
         */
        public boolean hmset(String key, Map<String, Object> map, long time) {
            try {
                redisTemplate.opsForHash().putAll(key, map);
                if (time > 0) {
                    expire(key, time);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 向一张hash表中放入数据,如果不存在将创建
         *
         * @param key   键
         * @param item  项
         * @param value 值
         * @return true 成功 false失败
         */
        public boolean hset(String key, String item, Object value) {
            try {
                redisTemplate.opsForHash().put(key, item, value);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 向一张hash表中放入数据,如果不存在将创建
         *
         * @param key   键
         * @param item  项
         * @param value 值
         * @param time  时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
         * @return true 成功 false失败
         */
        public boolean hset(String key, String item, Object value, long time) {
            try {
                redisTemplate.opsForHash().put(key, item, value);
                if (time > 0) {
                    expire(key, time);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 删除hash表中的值
         *
         * @param key  键 不能为null
         * @param item 项 可以使多个 不能为null
         */
        public void hdel(String key, Object... item) {
            redisTemplate.opsForHash().delete(key, item);
        }
    
        /**
         * 判断hash表中是否有该项的值
         *
         * @param key  键 不能为null
         * @param item 项 不能为null
         * @return true 存在 false不存在
         */
        public boolean hHasKey(String key, String item) {
            return redisTemplate.opsForHash().hasKey(key, item);
        }
    
        /**
         * hash递增 如果不存在,就会创建一个 并把新增后的值返回
         *
         * @param key  键
         * @param item 项
         * @param by   要增加几(大于0)
         * @return
         */
        public double hincr(String key, String item, double by) {
            return redisTemplate.opsForHash().increment(key, item, by);
        }
    
        /**
         * hash递减
         *
         * @param key  键
         * @param item 项
         * @param by   要减少记(小于0)
         * @return
         */
        public double hdecr(String key, String item, double by) {
            return redisTemplate.opsForHash().increment(key, item, -by);
        }
    
        /**
         * 根据key获取Set中的所有值
         *
         * @param key 键
         * @return
         */
        public Set<Object> sGet(String key) {
            try {
                return redisTemplate.opsForSet().members(key);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
        /**
         * 根据value从一个set中查询,是否存在
         *
         * @param key   键
         * @param value 值
         * @return true 存在 false不存在
         */
        public boolean sHasKey(String key, Object value) {
            try {
                return redisTemplate.opsForSet().isMember(key, value);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 将数据放入set缓存
         *
         * @param key    键
         * @param values 值 可以是多个
         * @return 成功个数
         */
        public long sSet(String key, Object... values) {
            try {
                return redisTemplate.opsForSet().add(key, values);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }
    
        /**
         * 将set数据放入缓存
         *
         * @param key    键
         * @param time   时间(秒)
         * @param values 值 可以是多个
         * @return 成功个数
         */
        public long sSetAndTime(String key, long time, Object... values) {
            try {
                Long count = redisTemplate.opsForSet().add(key, values);
                if (time > 0) {
                    expire(key, time);
                }
                return count;
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }
    
        /**
         * 获取set缓存的长度
         *
         * @param key 键
         * @return
         */
        public long sGetSetSize(String key) {
            try {
                return redisTemplate.opsForSet().size(key);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }
    
        /**
         * 移除值为value的
         *
         * @param key    键
         * @param values 值 可以是多个
         * @return 移除的个数
         */
        public long setRemove(String key, Object... values) {
            try {
                Long count = redisTemplate.opsForSet().remove(key, values);
                return count;
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }
        // ===============================list=================================
    
        /**
         * 获取list缓存的内容
         *
         * @param key   键
         * @param start 开始
         * @param end   结束 0 到 -1代表所有值
         * @return
         */
        public List<Object> lGet(String key, long start, long end) {
            try {
                return redisTemplate.opsForList().range(key, start, end);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
        /**
         * 获取list缓存的长度
         *
         * @param key 键
         * @return
         */
        public long lGetListSize(String key) {
            try {
                return redisTemplate.opsForList().size(key);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }
    
        /**
         * 通过索引 获取list中的值
         *
         * @param key   键
         * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
         * @return
         */
        public Object lGetIndex(String key, long index) {
            try {
                return redisTemplate.opsForList().index(key, index);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
        /**
         * 将list放入缓存
         *
         * @param key   键
         * @param value 值
         * @return
         */
        public boolean lSet(String key, Object value) {
            try {
                redisTemplate.opsForList().rightPush(key, value);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 将list放入缓存
         *
         * @param key   键
         * @param value 值
         * @param time  时间(秒)
         * @return
         */
        public boolean lSet(String key, Object value, long time) {
            try {
                redisTemplate.opsForList().rightPush(key, value);
                if (time > 0) {
                    expire(key, time);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 将list放入缓存
         *
         * @param key   键
         * @param value 值
         * @return
         */
        public boolean lSet(String key, List<Object> value) {
            try {
                redisTemplate.opsForList().rightPushAll(key, value);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 将list放入缓存
         *
         * @param key   键
         * @param value 值
         * @param time  时间(秒)
         * @return
         */
        public boolean lSet(String key, List<Object> value, long time) {
            try {
                redisTemplate.opsForList().rightPushAll(key, value);
                if (time > 0) {
                    expire(key, time);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 根据索引修改list中的某条数据
         *
         * @param key   键
         * @param index 索引
         * @param value 值
         * @return
         */
        public boolean lUpdateIndex(String key, long index, Object value) {
            try {
                redisTemplate.opsForList().set(key, index, value);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 移除N个值为value
         *
         * @param key   键
         * @param count 移除多少个
         * @param value 值
         * @return 移除的个数
         */
        public long lRemove(String key, long count, Object value) {
            try {
                Long remove = redisTemplate.opsForList().remove(key, count, value);
                return remove;
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }
    }
    

    自定义RedisTemplate序列化规则

    第一步:自定义一个类,实现RedisSerializer,并重写serialize()和deserialize()方法;
    第二步:在配置类中注册bean;

        @Bean
        public RedisTemplate<String, List<User>> userRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate<String, List<User>> template = new RedisTemplate<>();
            template.setConnectionFactory(redisConnectionFactory);
            template.setKeySerializer(new StringRedisSerializer());
            template.setValueSerializer(new 自定义序列化<>(User.class));
            return template;
        }
    

    第三步:就可以像使用RedisTemplate一样使用userRedisTemplate了。

    Redis客户端

    笔者使用的是第四个,AnotherRedisDesktopManager。

    1.RedisClient
    是否收费:免费
    项目介绍:Java 编写的 Redis 连接客户端,功能丰富,并且是免费的。
    支持平台:Windows
    项目地址:https://github.com/caoxinyu/RedisClient

    2.Redis Desktop Manager
    是否收费:收费
    项目介绍:一款基于 Qt5 的跨平台 Redis 桌面管理软件。
    支持平台:Windows、Mac OS、Linux
    项目地址:https://github.com/uglide/RedisDesktopManager

    3.RedisStudio
    是否收费:免费
    项目介绍:一款 C++ 编写的 Redis 管理工具,比较老,好久没更新了。
    支持平台:Windows
    项目地址:https://github.com/cinience/RedisStudio

    4.AnotherRedisDesktopManager
    是否收费:免费
    项目介绍:一款基于 NodeJS 开发的 Redis 桌面管理器,它的特点就是相对来说比较稳定,在数据量比较大的时候不会崩溃。
    支持平台:Windows、Mac OS、Linux
    项目地址:https://github.com/qishibo/AnotherRedisDesktopManager

    参考文献

    https://blog.csdn.net/tianwei7518/category_9263770.html
    StringRedisTemplate和RedisTemplate的区别(二)
    StringRedisTemplate的一个小案例(三)

  • 相关阅读:
    Python之路-(js正则表达式、前端页面的模板套用、Django基础)
    Python之路-jQuery
    单例模式实际应用
    hibernate联合主键注解配置
    spring:ContextLoaderListener接口
    Struts2 校验
    Control character in cookie value, consider BASE64 encoding your value-Cookie保存中文出错[转]
    基于Cookie跨域的单点登录问题
    Spring:启动项目时加载数据库数据(总结)
    Oracle表分区[转]
  • 原文地址:https://www.cnblogs.com/zllk/p/14570183.html
Copyright © 2011-2022 走看看