Redis简单记录

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() 关闭 归还到连接池中
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335