• Redis 数据类型


    Redis数据类型

    官网说明文档:http://www.redis.io/topics/data-types-intro

    Redis keys

    Redis keys are binary safe, this means that you can use any binary sequence as a key, from a string like "foo" to the content of a JPEG file. The empty string is also a valid key.

    Redis key是二进制安全的,这意味着你可以使用任何二进制序列作为一个key,从一个字符串”foo”到JPEG文件的内容。空字符串也是合法的key。

    规则

    • Key不要太长,消耗内存,在数据集中查找key可能会需要更多的匹配成本。
    • Key不要太短,可读性较差,
    • 追寻一种模式"object-type:id"  "comment:1234:reply.to" or "comment:1234:reply-to"
    • Key允许的最大值为512M

    Redis客户端 Redis-Cli

    查看帮助

    redis-cli  --help

    指定数据块编号

    redis-cli -n 0

    获取命令的帮助信息

    官网帮助文档:http://www.redis.io/commands

    help @string  获取数据类型string的相关操作命令

    help @list 获取数据类型list的相关操作命令

    help set

    help <tab>键 有提示信息

    String

    一个字符串类型的值最多能存储512M字节的内容

    这里简单介绍下String类型:

    help @string

      APPEND key value
      summary: Append a value to a key
      since: 2.0.0
    
      BITCOUNT key [start] [end]
      summary: Count set bits in a string
      since: 2.6.0
    
      BITOP operation destkey key [key ...]
      summary: Perform bitwise operations between strings
      since: 2.6.0
    
      BITPOS key bit [start] [end]
      summary: Find first bit set or clear in a string
      since: 2.8.7
    
      DECR key
      summary: Decrement the integer value of a key by one
      since: 1.0.0
    
      DECRBY key decrement
      summary: Decrement the integer value of a key by the given number
      since: 1.0.0
    
      GET key
      summary: Get the value of a key
      since: 1.0.0
    
      GETBIT key offset
      summary: Returns the bit value at offset in the string value stored at key
      since: 2.2.0
    
      GETRANGE key start end
      summary: Get a substring of the string stored at a key
      since: 2.4.0
    
      GETSET key value
      summary: Set the string value of a key and return its old value
      since: 1.0.0
    
      INCR key
      summary: Increment the integer value of a key by one
      since: 1.0.0
    
      INCRBY key increment
      summary: Increment the integer value of a key by the given amount
      since: 1.0.0
    
      INCRBYFLOAT key increment
      summary: Increment the float value of a key by the given amount
      since: 2.6.0
    
      MGET key [key ...]
      summary: Get the values of all the given keys
      since: 1.0.0
    
      MSET key value [key value ...]
      summary: Set multiple keys to multiple values
      since: 1.0.1
    
      MSETNX key value [key value ...]
      summary: Set multiple keys to multiple values, only if none of the keys exist
      since: 1.0.1
    
      PSETEX key milliseconds value
      summary: Set the value and expiration in milliseconds of a key
      since: 2.6.0
    
      SET key value [EX seconds] [PX milliseconds] [NX|XX]
      summary: Set the string value of a key
      since: 1.0.0
    
      SETBIT key offset value
      summary: Sets or clears the bit at offset in the string value stored at key
      since: 2.2.0
    
      SETEX key seconds value
      summary: Set the value and expiration of a key
      since: 2.0.0
    
      SETNX key value
      summary: Set the value of a key, only if the key does not exist
      since: 1.0.0
    
      SETRANGE key offset value
      summary: Overwrite part of a string at key starting at the specified offset
      since: 2.2.0
    
      STRLEN key
      summary: Get the length of the value stored in a key
      since: 2.2.0

    设置Key-value值

    SET key value [EX seconds] [PX milliseconds] [NX|XX]

    EX  过期时间,秒  等同于 SETEX key seconds value

    PX  过期时间,毫秒  等同于 PSETEX key milliseconds value

    NX  键不存在,才能设置  等同于 SETNX key value

    XX  键存在时,才能设置

    设置多个Key和value

    MSET key value [key value ...]
    MSETNX key value [key value ...]   键不存在时,才设置值

    过期设置

    设置多少秒或毫秒后过期

    EXPIRE key seconds    秒
    PEXPIRE key milliseconds  毫秒

    设置在指定unix时间戳过期

    EXPIREAT key timestamp     秒
    PEXPIREAT key milliseconds-timestamp    毫秒
    PERSIST key  删除过期的key

    查看剩余时间

    TTL key
    PTTL key

    -1 没有设置TTL

    -2找有找到key

    查找key

    KEYS pattern

    *任意长度字符

    ?任意一个字符

    []字符集合

    增量计数

    INCR key

    例子:

    set counter 100

    INCR counter

    101

    INCRBY key increment  根据给定的值进行增量计数
    DECR key
    DECRBY key increment 

    设置位值

    SETBIT key offset value

    offset偏移从0开始

    value值为0或1

    获取位置

    GETBIT key offset

    位所在位置

    BITPOS key bit [start] [end]

    start 值从0开始,偏移量为字节,不是位

    bit值为0或者1

    统计指定区间上值为1的个数

    BITCOUNT key [start] [end]

    start 值从0开始,偏移量为字节,不是位

    返回1的个数

    可以合理利用BIT操作来进行数据的统计

    按天统计网站活跃用户

    日期作为key,用户ID作为offset,上线置位1

    求一段时间内活跃用户数

    SETBIT 20160802 13 1     用户在哪天有上线记录
    SETBIT 20160801 12 1
    SETBIT 20160803 12 1
    BITOP OR 20160801-3 20160801 20160802 20160803 这期间有哪些用户有上线记录,针对重复的不进行累计,只做记录
    BITCOUNT 20160801-3  获得统计结果,有多少用户在这期间登录过
  • 相关阅读:
    crontab使用
    python 学习 第一课
    php调用阿里大鱼 接口curl
    thinkphp 动态 级联
    nginx重启
    linux查看 文件夹大小
    mysql convert
    mysql 数据库导入 导出,解决 导入 错误问题
    .net 更新数据 ado.net parameter
    PHP的超全局变量$_SERVER
  • 原文地址:https://www.cnblogs.com/one--way/p/5733833.html
Copyright © 2020-2023  润新知