美文网首页
Redis简单记录

Redis简单记录

作者: livesxu | 来源:发表于2020-09-26 13:35 被阅读0次

    1.什么是Redis

    一个开源的使用C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。非关系型数据库,数据之间没有关系,数据存储在内存中
    https://www.redis.net.cn
    目前Redis支持的键值数据类型如下:

    • 字符串类型 string
    • 哈希类型 hash
    • 列表类型 list
    • 集合类型 set
    • 有序集合类型 sortedset

    Mac在src目录下:
    启动服务:redis-server
    交互操作:redis-cli

    2.持久化 redis.conf中配置。

    • RDB:在一定的间隔时间内,检测key的变化情况,然后持久化数据。默认方式。
    # Save the DB on disk:
    #
    #   save <seconds> <changes>
    #
    #   Will save the DB if both the given number of seconds and the given
    #   number of write operations against the DB occurred.
    #
    #   In the example below the behaviour will be to save:
    #   after 900 sec (15 min) if at least 1 key changed
    #   after 300 sec (5 min) if at least 10 keys changed
    #   after 60 sec if at least 10000 keys changed
    #
    #   Note: you can disable saving completely by commenting out all "save" lines.
    #
    #   It is also possible to remove all the previously configured save
    #   points by adding a save directive with a single empty string argument
    #   like in the following example:
    #
    #   save ""
    
    save 900 1
    save 300 10
    save 60 10000
    
    • AOF:日志记录的方式,可以记录每一条命令的操作。可以每一次命令操作后,持久化数据。
      appendonly no(关闭aof) ——> appendonly yes(开启aof)
    # AOF and RDB persistence can be enabled at the same time without problems.
    # If the AOF is enabled on startup Redis will load the AOF, that is the file
    # with the better durability guarantees.
    #
    # Please check http://redis.io/topics/persistence for more information.
    
    appendonly no
    
    # The name of the append only file (default: "appendonly.aof")
    
    appendfilename "appendonly.aof"
    
    # The fsync() call tells the Operating System to actually write data on disk
    # instead of waiting for more data in the output buffer. Some OS will really flush
    # data on disk, some other OS will just try to do it ASAP.
    #
    # Redis supports three different modes:
    #
    # no: don't fsync, just let the OS flush the data when it wants. Faster.
    # always: fsync after every write to the append only log. Slow, Safest.
    # everysec: fsync only one time every second. Compromise.
    #
    # The default is "everysec", as that's usually the right compromise between
    # speed and data safety. It's up to you to understand if you can relax this to
    # "no" that will let the operating system flush the output buffer when
    # it wants, for better performances (but if you can live with the idea of
    # some data loss consider the default persistence mode that's snapshotting),
    # or on the contrary, use "always" that's very slow but a bit safer than
    # everysec.
    #
    # More details please check the following article:
    # http://antirez.com/post/redis-persistence-demystified.html
    #
    # If unsure, use "everysec".
    
    # appendfsync always 每一次操作都进行持久化
    appendfsync everysec 每隔一秒进行一次持久化
    # appendfsync no 不进行持久化
    

    3.应用场景

    • 缓存(数据查询、短链接、新闻内容、商品内容等)
    • 任务队列(秒杀,抢购)
    • 榜单/排行榜
    • 网站访问统计
    • 数据过期处理(短信验证码过期)
    • 分布式集群架构中的session分离
    • 聊天室的在线好友列表

    4.连接池

    public class RedisUtils {
    
        private RedisUtils(){}
    
        private static JedisPool jedisPool = null;
    
        static {
    
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(50);
            config.setMaxIdle(10);
    
            jedisPool = new JedisPool(config,"localhost",6379);
        }
    
        static public Jedis getRedis () {
    
            return jedisPool.getResource();
        }
    }
    

    5.示例:

    //    https://www.redis.net.cn
    //    https://mvnrepository.com/artifact/redis.clients/jedis
    //    https://www.redis.net.cn/tutorial/3507.html
        @Test
        public void test1 () {
    
            RedisUtils.getRedis().set("oneKey","some thing");
    
            String oneValue = RedisUtils.getRedis().get("oneKey");
            //some thing
            System.out.println(oneValue);
    
            RedisUtils.getRedis().del("oneKey");
    
            RedisUtils.getRedis().hset("oneHashKey","name","zhangsan");
    
            String hNameValue = RedisUtils.getRedis().hget("oneHashKey","name");
            //zhangsan
            System.out.println(hNameValue);
    
            RedisUtils.getRedis().hdel("oneHashKey","name");
    
            //list,左侧添加,结果打印应该是cba,因为是依次添加
            RedisUtils.getRedis().lpush("oneListKey","a","b","c");
            //list,右侧添加
            RedisUtils.getRedis().rpush("oneListKey","a","b","c");
    
            //0到-1 查询所有
            List<String> oneListValues = RedisUtils.getRedis().lrange("oneListKey",0,-1);
            //[c, b, a, a, b, c]
            System.out.println(oneListValues);
    
            String rOne = RedisUtils.getRedis().rpop("oneListKey");
            //c
            System.out.println(rOne);
    
            RedisUtils.getRedis().sadd("oneSetKey","a","b","c");
    
            RedisUtils.getRedis().srem("oneSetKey","a");
    
            Set<String> oneSetValues = RedisUtils.getRedis().smembers("oneSetKey");
            //[c, b]
            System.out.println(oneSetValues);
    
            //有序set 一般用在排行榜
    
            RedisUtils.getRedis().zadd("oneSortedSetKey",100,"a");
            RedisUtils.getRedis().zadd("oneSortedSetKey",80,"b");
            RedisUtils.getRedis().zadd("oneSortedSetKey",90,"c");
    
            Set<String> oneSortedSetValues = RedisUtils.getRedis().zrange("oneSortedSetKey",0,-1);
            //[b, c, a] 按照score的数值从低到高排列的结果
            System.out.println(oneSortedSetValues);
    
            Double bScore = RedisUtils.getRedis().zscore("oneSortedSetKey","b");
            //80.0
            System.out.println(bScore);
    
            //插入一个key,过期时间10秒,一般用在 验证码
            RedisUtils.getRedis().setex("oneOverTimeKey",10,"oneOverTimeValue");
    
            //查询所有的键 指令 keys *
            RedisUtils.getRedis().keys("*");
    
            //获取指定key的类型 指令 type key
            RedisUtils.getRedis().type("oneHashKey");
    
            //删除指定key 指令 del key
            RedisUtils.getRedis().del("oneHashKey");
    
            //close() 关闭 归还到连接池中
        }
    

    相关文章

      网友评论

          本文标题:Redis简单记录

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