前言 公司手游使用redis作为数据库,虽然做的是测试工作,但还是觉得有必要了解一些redis的相关概念以及基本操作。以前做页游测试时涉及到memcache,Tokyo Tyrant但使用的是别人开发好的工具,了解几乎为零。了解以下命令,可以完成基本的查询插入操作。
一、基本认识: redis本质上是一个Key-Value类型的内存数据库,扩展了memcache。整个数据库全部加载在内存中进行操作,并且定期通过异步操作把数据库数据flush到硬盘上保存。 1 redis可以看做Memcache的扩展,基于内存并支持数据持久化存储。 2 支持网络、日志型、Key-Value型数据库 3 支持常规数值、字符串,Lists、Sets、Sorted Sets、Hashes。 4 获取帮助信息:help command
二、基本操作:
set key value | 设置key及其value | get key | 查询数据 | del key | 删除键值 | exists key | 验证键值是否存在,0代表不存在,1代表存在 | setnx key | key已经存在返回0,不存在返回1并设置成功 | setex key | 有效期10秒钟:setex name jake 10 | setrange | 设置指定key的子字符串:setrange key 8 163.com 将key的第八个字符及其后7个字符的串替换为163.co | mset key1 key2… | 一次设置多个key | mget key1 key2… | 一次获取多个键的值 | msetnx key1 key2… | 一次设置多个键值对并判断是否已存在 | getset key | 设置key的值,并返回key的旧值 | getrange key n1 n2 | 获取指定key指定范围的子字符串
最后的字符是-1,依次向前递减
| incr key | 值加1 | incrby key intValue | 加上指定值 | decr | 值减1 | decrby key intValue | 减去指定值 | append key string | 追加字符串在末尾 | strlen key | 获取指定字符串的长度 |
三、hash表操作,特别使用于存储对象:
1、hset key field value
summary: Set the string value of a hash field
2、hsetnx key field value
summary: Set the value of a hash field, only if the field does not exist
3、hmset key field value [field value ...]
summary: Set multiple hash fields to multiple values
4、hget key field
summary: Get the value of a hash field
5、hmget key field [field ...]
summary: Get the values of all the given hash fields
6、hincrby key field increment
summary: Increment the integer value of a hash field by the given number
7、hexists key field
summary: Determine if a hash field exists
8、hlen key
summary: Get the number of fields in a hash
9、hdel key key field [field ...]
summary: Delete one or more hash fields
10、hkeys
summary: Get all the fields in a hash
11、hvals
summary: Get all the values in a hash
12、hgetall
summary: Get all the fields and values in a hash
|