永远年轻,永远热泪盈眶!
分类: 服务器与存储
2016-06-22 11:01:09
#Redis 操作指南
* 目录:
一、Redis 键操作
1.1 /sync/redis_key_del 删除Key(Key对应的数据也会被删除),对所有的容器都适用
二、Redis 字符串操作
2.1 /sync/redis_string_set 设置key value
2.2 /sync/redis_string_get 根据key查询value
三、Redis 哈希操作
3.1 /sync/redis_hash_get 根据key field查询value
3.2 /sync/redis_hash_getall 根据key查询所有的field value
3.3 /sync/redis_hash_set 设置hash的数据,key对应多条field,value
3.4 /sync/redis_hash_del 删除Key、field对应的数据
四、Redis 列表操作
4.1 /sync/redis_lists_llen 获取列表的长度
4.2 /sync/redis_lists_lpop 获取列表中的第一个元素,并把该元素从列表中删除
4.3 /sync/redis_lists_lpush 在列表前面(从左边)添加一个元素
4.4 /sync/redis_lists_rpop 获取列表中的最后一个元素,并把该元素从列表中删除
4.5 /sync/redis_lists_rpush 在列表尾部(从右边)添加一个元素
4.6 /sync/redis_lists_lrange 获取列表中指定范围内的元素
#一、redis 键操作
###1.1 删除Key(Key对应的数据也会被删除),对所有的容器都适用
* URL: /sync/redis_key_del
* Method: GET
* Parameter: key(string)
* Return:
* [HTTP header if return 406]:
> 删除失败
* [HTTP body if return OK]:
> return 200 //如果key不存在也返回200
* Example:
> /sync/redis_key_del?key=xxx
>
> Return:
> HTTP/1.1 200 OK
> Content-Length: 0
> Content-Type: text/plain
#二、Redis 字符串操作
###2.1 设置key value
* URL: /sync/redis_string_set
* Method: POST
* Parameter: key(string),expire_time(单位:秒)
* POST body:
* BODY: string 或 JSON 字符串
> "hello_world"
* Note:
> key已经存在的话,会更新掉原有的value
* Return:
* [HTTP header if return 406]:
> 设置失败
* [HTTP body if return OK]:
> 设置成功则返回http status 200
* Example:
> /sync/redis_string_set?key=user_id&expire_time=3600
> "hello_linux"
>
> Return:
> HTTP/1.1 200 OK
> Date: Thu, 30 Jul 2015 03:22:16 GMT
> Content-Type: text/plain
> Transfer-Encoding: chunked
> Connection: keep-alive
> Server: Intsig Web Server
###2.2 根据key查询value
* URL: /sync/redis_string_get
* Method: GET
* Parameter: key(string)
* Return:
* [HTTP header if return 406]:
> 查询失败,没有数据
* [HTTP body if return OK]:
> 返回该key对应的value
> return body:
> xxx
* Example:
> /sync/redis_string_get?key=user_id
> Return:
> HTTP/1.1 200 OK
> Date: Thu, 30 Jul 2015 03:28:16 GMT
> Content-Type: text/plain
> Connection: keep-alive
> content-length: 9
> Server: Intsig Web Server
>
> hello_linux
#三、Redis 哈希操作
###3.1 根据key field查询value
* URL: /sync/redis_hash_get
* Method: GET
* Parameter: key(string),field(string)
* Return:
* [HTTP header if return 406]:
> 查询失败,没有数据
* [HTTP body if return OK]:
> 返回返回该key对应的数据。
> return body:
> xxx
* Example:
> /sync/redis_hash_get?key=user_id&field=name
> HTTP/1.1 200 OK
> Date: Thu, 30 Jul 2015 07:32:14 GMT
> Content-Type: text/plain
> Connection: keep-alive
> content-length: 6
> Server: Intsig Web Server
>
> jason_laing
###3.2 根据key查询所有的field value
* URL: /sync/redis_hash_getall
* Method: GET
* Parameter: key(string)
* Return:
* [HTTP header if return 406]:
> 查询失败,没有数据
* [HTTP body if return OK]:
> 返回该key对应所有field数据,JSON格式
> return body:
> {
> "field1": "helloword",
> "field2": { "abc":"sdfasf", "afsdfas":2342},
> ...
> }
* Example:
> /sync/redis_hash_getall?key=user_id
> Return:
> HTTP/1.1 200 OK
> Content-Length: 356
> Content-Type: text/plain
> {
> "field1": "helloword",
> "field2": {"abc":"sdfasf","afsdfas":2342},
> ...
> }
###3.3 设置hash的数据,key对应多条field,value
* URL: /sync/redis_hash_set
* Method: POST
* Parameter: key(string),expire_time(单位:秒)
* POST body:
* BODY: body为JSON格式
> {
> "field1": "helloword",
> "field2": {"abc":"sdfasf","afsdfas":2342},
> ...
> }
* note:
> reids中所有的expiretime是顶级key的属性,所有的field共享一个过期时间
* Return:
* [HTTP header if return 406]:
> 设置失败
* [HTTP body if return OK]:
> 1:设置成功则返回http status 200
> 2:若该field已经存在,会更新;不存在,表示添加
* Example:
> /sync/redis_hash_set?key=user_id&expire_time=3600
> POST body:
> {
> "field1": "helloword",
> "field2": {"abc":"sdfasf","afsdfas":2342},
> ...
> }
>
> Return:
>
> HTTP/1.1 200 OK
> Date: Thu, 30 Jul 2015 03:43:59 GMT
> Content-Type: text/plain
> Transfer-Encoding: chunked
> Connection: keep-alive
> Server: Intsig Web Server
###3.4 删除Key、field对应的数据
* URL: /sync/redis_hash_del
* Method: GET
* Parameter: key(string),field(string)
* Return:
* [HTTP header if return 406]:
> 删除失败
* [HTTP body if return OK]:
> return 200 //key不存在或者field不存在,返回200
* Example:
> /sync/redis_hash_del?key=user_id&field=name
>
> Return:
> HTTP/1.1 200 OK
> Content-Length: 0
> Content-Type: text/plain
#四、Redis 列表操作
###4.1 获取列表的长度
* URL: /sync/redis_lists_llen
* Method: GET
* Parameter: key
* Return:
* [HTTP header if return 406]:
> 获取失败,没有数据
* [HTTP body if return OK]:
> 返回该key对应的列表长度
> return body:
> xxx
* Example:
> /sync/redis_lists_llen?key=user_id
> Return:
> HTTP/1.1 200 OK
> Date: Thu, 30 Jul 2015 03:28:16 GMT
> Content-Type: text/plain
> Connection: keep-alive
> content-length: 9
> Server: Intsig Web Server
>
> 9
###4.2 获取列表中的第一个元素,并把该元素从列表中删除
* URL: /sync/redis_lists_lpop
* Method: GET
* Parameter: key
* Return:
* [HTTP header if return 406]:
> 获取失败,没有数据
* [HTTP body if return OK]:
> 返回该key对应的列表的第一个元素
> return body:
> "person_basic_info"
* Example:
> /sync/redis_lists_lpop?key=user_id
> Return:
> HTTP/1.1 200 OK
> Date: Thu, 30 Jul 2015 03:28:16 GMT
> Content-Type: text/plain
> Connection: keep-alive
> content-length: 9
> Server: Intsig Web Server
>
> "person_basic_info"
###4.3 在列表前面(从左边)添加一个元素
* URL: /sync/redis_lists_lpush
* Method: POST
* Parameter: key
* POST body:
* BODY: string 或 JSON 字符串
> "person_basic_info"
* Return:
* [HTTP header if return 406]:
> 添加失败
* [HTTP body if return OK]:
> 设置成功则返回 http status 200
* Example:
> /sync/redis_lists_lpush?key=user_id
> "person_basic_info"
>
> Return:
> HTTP/1.1 200 OK
> Date: Thu, 30 Jul 2015 03:22:16 GMT
> Content-Type: text/plain
> Transfer-Encoding: chunked
> Connection: keep-alive
> Server: Intsig Web Server
###4.4 获取列表中的最后一个元素,并把该元素从列表中删除
* URL: /sync/redis_lists_rpop
* Method: GET
* Parameter: key
* Return:
* [HTTP header if return 406]:
> 获取失败,没有数据
* [HTTP body if return OK]:
> 返回该key对应的列表的最后一个元素
> return body:
> "person_basic_info"
* Example:
> /sync/redis_lists_rpop?key=user_id
> Return:
> HTTP/1.1 200 OK
> Date: Thu, 30 Jul 2015 03:28:16 GMT
> Content-Type: text/plain
> Connection: keep-alive
> content-length: 9
> Server: Intsig Web Server
>
> "person_basic_info"
###4.5 在列表尾部(从右边)添加一个元素
* URL: /sync/redis_lists_rpush
* Method: POST
* Parameter: key
* POST body:
* BODY: string 或 JSON 字符串
> "person_education_info"
* Return:
* [HTTP header if return 406]:
> 添加失败
* [HTTP body if return OK]:
> 设置成功则返回 http status 200
* Example:
> /sync/redis_lists_rpush?key=user_id
> "person_education_info"
>
> Return:
> HTTP/1.1 200 OK
> Date: Thu, 30 Jul 2015 03:22:16 GMT
> Content-Type: text/plain
> Transfer-Encoding: chunked
> Connection: keep-alive
> Server: Intsig Web Server
###4.6 获取列表中指定范围内的元素
* URL: /sync/redis_lists_lrange
* Method: GET
* Parameter: key,start,stop
* Return:
* [HTTP header if return 406]:
> 获取失败,没有数据
* [HTTP body if return OK]:
> 返回该key对应列表的指定范围内的元素
> return body:
> ["person_basic_info","person_education_info",...]
* Example:
> /sync/redis_lists_rpop?key=user_id&start=0&stop=4
> Return:
> HTTP/1.1 200 OK
> Date: Thu, 30 Jul 2015 03:28:16 GMT
> Content-Type: text/plain
> Connection: keep-alive
> content-length: 9
> Server: Intsig Web Server
>
> ["person_basic_info","person_education_info",...]