layout: post
title: Redis简介与基础命令
date: 2018-03-22
tags: [“Redis”,”软件服务”]
一、Redis简介
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries.
Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
注意:只有3.x的版本支持redis-cluster功能,老点的2.x并不支持(cluster非replication)
二、data structure base command
1、string类型value的操作
- SET 设置字符串键值
- GET 获取字符串键值
- EXISTS 判断键值是否存在
- INCR 当前值加1(inter类型)
- DECR 当前值减1(inter类型)
- SETNX Set the value of a key, only if the key does not exist
- SETEX 设置密钥的值和到期日期
- INCRBYFLOAT Increment the float value of a key by the given amount
- MGET 获取多个值
- MSET 设置多个值
2、list类型value的操作
- LPUSH 在栈左方插入数据
- RPUSH 在栈右方插入数据
- LPOP 左方栈弹出数据
- RPOP 右方栈弹出数据
- LPUSHX Prepend a value to a list, only if the list exists
- RPUSHX Prepend a value to a list, only if the list exists
- LRANGE Get a range of elements from a list
- LINDEX Get an element from a list by its index
- LSET Set the value of an element in a list by its index
3、set类型value的操作
- SADD 添加集合
- SPOP 从集合中随机弹出元素
- SREM Remove one or more members from a set
- SRANDMEMBER Get one or multiple random members from a set
- SINTER Intersect multiple sets(交集)
- SUNION 并集
4、sort set 类型value的操作
- ZADD Add one or more members to a sorted set, or update its score if it already exists
- ZCARD Get the number of members in a sorted set
- ZCOUNT Count the members in a sorted set with scores within the given values
- ZRANK Determine the index of a member in a sorted set
5、hash类型value的操作
- HSET Set the string value of a hash field
- HMSET Set multiple hash fields to multiple values
- HGET Get the value of a hash field
- HMGET Get the values of all the given hash fields
- HKEYS Get all the fields in a hash
- HVALS Get all the values in a hash
- HDEL Delete one or more hash fields
- HGETALL Get all the fields and values in a hash
6、pubsub
- PUBLISH Post a message to a channel
- SUBSCRIBE Listen for messages published to the given channels
- UNSUBSCRIBE Stop listening for messages posted to the given channels
- PSUBSCRIBE Listen for messages published to channels matching the given patterns
- PUNSUBSCRIBE Stop listening for messages posted to channels matching the given patterns