main content:
1. Introduction to Redis
2. Similarities and differences between Redis and other software
3. Usage of RedisIntroduction to Redis
Redis is a very fast non-relational database that can store mappings between keys and 5 different types of values, can persist key-value pair data stored in memory to hard disk, and can use replication features to expand read performance, You can also use client-side sharding to scale write performance.
1. Comparison of Redis with other databases and software
name | Types of | Data storage options | query type | Additional features |
---|---|---|---|---|
Say it again | Non-relational database using in-memory storage | Strings, lists, sets, hashes, sorted sets | Each data type has its own dedicated commands, plus bulk operations and incomplete transaction support | Publish and subscribe, master-slave replication, persistence, scripting |
memcached | Key-value cache using in-memory storage | Mapping between keys and values | create command, read command command, update command, delete command, and several other commands |
Multithreaded server for improved performance |
MySql | relational database | Each database can contain multiple tables, each table can contain multiple rows, can handle views of multiple tables, supports spatial and third-party extensions | select, insert, update, delete, function, stored procedure |
Supports ACID properties, master-slave replication and master-master replication |
MongoDB | Non-relational document storage using hard disk storage | Each database can contain multiple tables, and each table can contain multiple schemaless BSON documents | Create command, read command, update command, delete command, conditional query command, etc. | Support map-reduce operations, master-slave replication, sharding, spatial indexing |
2. Introduction to Redis data structure
The five data structure types are: String, LIST, SET, HASH, ZSET
type of data | the value stored in the structure | Structural literacy |
---|---|---|
STRING | Can be a string, integer or float | Operates on entire strings or parts of strings, and increments or decrements integers and floating-point numbers |
LIST | A linked list, each node on the linked list contains a string | Push or pop elements from both ends of the linked list, trim the linked list by offset, read single or multiple elements, find or remove elements by value |
SET | An unordered collector that contains strings, and each contained string is unique and distinct | Add, get, remove individual elements, check if an element exists in the set, compute intersection, and 集、差集,从集合中随机获取元素 |
HASH | 包含键值的无序散列表 | 添加、获取、移除单个键值对,获取所有键值对 |
ZSET | 字符串成员与浮点数分值之间的有序映射,元素的排序由分值的大小决定 | 添加、获取、删除单个元素,根据分值范围或者成员来获取元素 |
Java代码可以从https://github.com/josiahcarlson/redis-in-action/tree/master/java下载
字符串STRING常用的命令:
GET :获取存储在给定键中的值
SET : 设置存储在给定键中的值
DEL:删除存储在给定键中的值
一个字符串示例,键为hello,值为world
列表LIST常用的命令
LPUSH:将元素推入列表的左端
RPUSH:将元素推入列表的右端
LPOP:从列表的左端弹出元素
RPOP:从列表的右端弹出元素
LINDEX:获取列表再给定位置上的单个元素
LRANGE:获取列表再给定范围上的所有元素
SET集合的命令
SADD:将给定元素添加到集合
SMEMBERS:返回集合包含的所有元素
SISMEMBER:检查给定元素是否存在于集合中
SREM:如果给定的元素存在于集合中,则移除这个元素
HASH常用的命令
HSET:在散列里面关联起给定的键值对
HGET:获取指定散列键的值
HGETALL:获取散列包含的所有键值对
HDEL:如果给定键存在于散列里面,那么移除这个键
有序集合ZSET常用的命令
ZADD:将一个带有给定分值的成员添加到有序集合里面
ZRANGE:根据元素在有序列表中所处的位置,从有序集合里面获取多个元素
ZRANFEBYSCORE:获取有序集合在给定分值范围内的所有元素
ZREM:如果给定成员存在于有序集合,那么移除这个成员