安装:
1. 下载的windows版本是redis-2.0.2,解压到D盘下:D:\redis-2.0.2
2. 启动Redis服务(conf文件指定配置文件,若不指定则默认):D:\redis-2.0.2>redis-server.exe redis.conf
启动cmd窗口要一直开着,关闭后则Redis服务关闭。
redis-server.exe:服务程序
redis-check-dump.exe:本地数据库检查
redis-check-aof.exe:更新日志检查
redis-benchmark.exe:性能测试,用以模拟同时由N个客户端发送M个 SETs/GETs查询 (类似于 Apache的ab 工具).
3. 这时服务开启着,另外开一个窗口进行,设置客户端:D:\redis-2.0.2>redis-cli.exe -h 202.117.16.133 -p 6379
使用:
在线使用文档:http://redis.readthedocs.org/en/2.4/
一. 使用java操作redis:
1. 下载jedis-2.0.0.jar.该包为java的客户端。
2. 测试代码
packagecom.mangocity.test;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importredis.clients.jedis.Jedis;
publicclass TestRedis {
/**
* @param args
*/
public static void main(String[] args){
//连接redis服务
Jedis jedis = newJedis("127.0.0.1",6379);
//密码验证-如果你没有设置redis密码可不验证即可使用相关命令
//jedis.auth("abcdefg");
//简单的key-value存储
jedis.set("redis","myredis");
System.out.println(jedis.get("redis"));
//删除一个key
//在原有值得基础上添加,如若之前没有该key,则导入该key
//之前已经设定了redis对应"myredis",此句执行便会使redis对应"myredisyourredis"
jedis.append("redis","yourredis");
jedis.append("content","rabbit");
jedis.del("redis");
//mset 是设置多个key-value值 参数(key1,value1,key2,value2,...,keyn,valuen)
//mget 是获取多个key所对应的value值 参数(key1,key2,key3,...,keyn) 返回的是个list
jedis.mset("name1","yangw","name2","demon","name3","elena");
System.out.println(jedis.mget("name1","name2","name3"));
//map
Map<String,String> user = newHashMap<String,String>();
user.put("name","cd");
user.put("password","123456");
//map存入redis
jedis.hmset("user",user);
//mapkey个数
System.out.println(String.format("len:%d",jedis.hlen("user")));
//map中的所有键值
System.out.println(String.format("keys:%s", jedis.hkeys("user") ));
//map中的所有value
System.out.println(String.format("values: %s",jedis.hvals("user") ));
//取出map中的name字段值
List<String> rsmap =jedis.hmget("user", "name","password");
System.out.println(rsmap);
//删除map中的某一个键值password
jedis.hdel("user","password");
System.out.println(jedis.hmget("user", "name","password"));
//list
jedis.del("listDemo");
System.out.println(jedis.lrange("listDemo", 0, -1));
jedis.lpush("listDemo","A");
jedis.lpush("listDemo","B");
jedis.lpush("listDemo","C");
System.out.println(jedis.lrange("listDemo", 0, -1));
System.out.println(jedis.lrange("listDemo", 0, 1));
//set
jedis.sadd("sname","wobby");
jedis.sadd("sname","kings");
jedis.sadd("sname","demon");
System.out.println(String.format("set num: %d",jedis.scard("sname")));
System.out.println(String.format("all members: %s",jedis.smembers("sname")));
System.out.println(String.format("is member: %B",jedis.sismember("sname", "wobby")));
System.out.println(String.format("randmember: %s", jedis.srandmember("sname")));
//删除一个对象
jedis.srem("sname","demon");
System.out.println(String.format("all members: %s",jedis.smembers("sname")));
}
}
二. 使用原始命令操作redis
1 hset命令
对hash中的字段赋值,如果不存在则创建否则覆盖
返回值:被创建返回1,被更新返回0
例子:
[php] view plaincopy
01.redis 127.0.0.1:6379> hset debughash field1 "hello"
02.(integer) 1
03.redis 127.0.0.1:6379> hget debughash field1
04."hello"
05.redis 127.0.0.1:6379> hset debughash field1 "world"
06.(integer) 0
07.redis 127.0.0.1:6379> hget debughash field1
08."world"
09.redis 127.0.0.1:6379>
2 hget命令
获取field对于的value
返回值:存在返回value值,否则返回 nil
例子:
[php] view plaincopy
01.redis 127.0.0.1:6379> hset debughash field2 "hello"
02.(integer) 1
03.redis 127.0.0.1:6379> hget debughash field2
04."hello"
05.redis 127.0.0.1:6379> hget debughash field3
06.(nil)
3 hexists
判断field是否存在
返回值:存在返回1,否则返回0
例子:
[php] view plaincopy
01.redis 127.0.0.1:6379> hexists debughash field2
02.(integer) 1
03.redis 127.0.0.1:6379> hexists debughash field3
04.(integer) 0
4 hdel
删除field
返回值:返回删除的数量
例子:
[php] view plaincopy
01.redis 127.0.0.1:6379> hdel debughash field2
02.(integer) 1
03.redis 127.0.0.1:6379> hdel debughash field3
04.(integer) 0
05.redis 127.0.0.1:6379> hget debughash field3
06.(nil)
5 hkeys
返回hash下包含的field
例子:
[php] view plaincopy
01.redis 127.0.0.1:6379> hkeys debughash
02.1) "field1"
03.redis 127.0.0.1:6379> hset debughash field2 "word"
04.(integer) 1
05.redis 127.0.0.1:6379> HKEYS debughash
06.1) "field1"
07.2) "field2"
08.redis 127.0.0.1:6379> HKEYS debughash field2
09.(error) ERR wrong number of arguments for 'hkeys' command
10.redis 127.0.0.1:6379> HKEYS debughash
11.1) "field1"
12.2) "field2"
13.redis 127.0.0.1:6379>
6 hlen
返回hash下含有field的数量
例子:
[php] view plaincopy
01.redis 127.0.0.1:6379> hlen debughash
02.(integer) 2
7 hmset
设置多个field值,存在覆盖,否则创建
例子:
[php] view plaincopy
01.redis 127.0.0.1:6379> hmset testhash field1 "hello" field2"world"
02.OK
03.redis 127.0.0.1:6379> hget testhash field1
04."hello"
05.redis 127.0.0.1:6379> hget testhash field2
06."world"
8 hmget
获取多个field的值
例子:
[php] view plaincopy
01.redis 127.0.0.1:6379> hmget testhash field1 field2 field3
02.1) "hello"
03.2) "world"
04.3) (nil)
9 hgetall
获取hash下所有field与value
例子:
[php] view plaincopy
01.redis 127.0.0.1:6379> hgetall testhash
02.1) "field1"
03.2) "hello"
04.3) "field2"
05.4) "world"
10 hvals
获取value值
例子:
[php] view plaincopy
01.redis 127.0.0.1:6379> hvals testhash
02.1) "hello"
03.2) "world"
11 hsetnx
只有field不存在的时候,设置value
返回值:如果创建成功返回1,若存在返回0
例子:
[php] view plaincopy
01.redis 127.0.0.1:6379> hgetall testhash
02.1) "field1"
03.2) "hello"
04.3) "field2"
05.4) "world"
06.redis 127.0.0.1:6379> hsetnx testhash field3 "00000"
07.(integer) 1
08.redis 127.0.0.1:6379> hgetall testhash
09.1) "field1"
10.2) "hello"
11.3) "field2"
12.4) "world"
13.5) "field3"
14.6) "00000"
15.redis 127.0.0.1:6379>
12 hincrby
按整数自增,如果不存在则field赋值为value,如果不是数值型的字符串,返回error
返回值:返回自增后的值
例子:
[php] view plaincopy
01.redis 127.0.0.1:6379> hgetall testhash
02.1) "field1"
03.2) "hello"
04.3) "field2"
05.4) "world"
06.5) "field3"
07.6) "00000"
08.redis 127.0.0.1:6379> hincrby testhash field4 5
09.(integer) 5
10.redis 127.0.0.1:6379> hicrby testhash field4 5
11.(error) ERR unknown command 'hicrby'
12.redis 127.0.0.1:6379> hincrby testhash field4 5
13.(integer) 10
14.redis 127.0.0.1:6379> hincrby testhash field4 -11
15.(integer) -1
16.redis 127.0.0.1:6379> hset testhash field5 "yy"
17.(integer) 1
18.redis 127.0.0.1:6379> hincrby testhash field5 -11
19.(error) ERR hash value is not an integer
20.redis 127.0.0.1:6379>