Redis简介 Linux安装Redis Redis使用
其他一些操作(包括 APPEND、GETRANGE、MSET 和 STRLENGTH 也可用于字符串。请参见http://doc.redisfans.com/string/index.html )
使用 LRANGE 命令查看列表中顶部的三个单词:
redis> LRANGE words ) ) )
删除:
#删除所有 socialism
redis> LREM words
删除列表:
redis > DEL words
(integer)
) ) ) ) ) > ZCARD wordswithlength
(integer)
) ) ) ) "gulati"
Redis IDE 工具使用(RedisDesktopManager)
1. 下载
链接:http://pan.baidu.com/s/1bnwg0OZ 密码:ekwu
使用java客户端 jedis操作redis
pom
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency>
测试代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
import java.util.HashMap; import java.util.List; import java.util.Map; import redis.clients.jedis.Jedis; public class TestRedis { private Jedis jedis; public TestRedis() { jedis = new Jedis( "123.59.42.xx" , 6379 ); // 权限认证 // jedis.auth("admin"); } /** * redis存储字符串 */ public void testString() { jedis.set( "name" , "Jerome苏" ); jedis.expire( "name" , 60 ); // 设置存活时间 // jedis.del("name"); //删除某个键 System.out.println( "name----->>>" + jedis.get( "name" )); } /** * redis操作Map */ public void testMap() { Map<String, String> map = new HashMap<String, String>(); map.put( "name" , "jerome" ); map.put( "age" , "24" ); map.put( "email" , "jerome_s@qq.com" ); jedis.hmset( "user" , map); // 存入redis // 取出 List<String> rsmap = jedis.hmget( "user" , "name" , "age" ); System.out.println(rsmap); // 删除map中的某个键值 // jedis.hdel("user", "age"); // System.out.println(jedis.hlen("user")); // 返回key为user的键中存放的值的个数2 // System.out.println(jedis.exists("user"));// 是否存在key为user的记录 返回true // System.out.println(jedis.hkeys("user"));// 返回map对象中的所有key // System.out.println(jedis.hvals("user"));// 返回map对象中的所有value } /** * jedis操作List */ public void testList() { // 开始前,先移除所有的内容 jedis.del( "animals" ); // -1表示取得所有 System.out.println(jedis.lrange( "animals" , 0 , - 1 )); // 存入List jedis.lpush( "animals" , "cat" ); jedis.lpush( "animals" , "dog" ); jedis.lpush( "animals" , "tiger" ); System.out.println(jedis.lrange( "animals" , 0 , - 1 )); // 删除单个 jedis.rpush( "animals" , "cat" ); System.out.println(jedis.lrange( "animals" , 0 , - 1 )); } /** * jedis操作Set */ public void testSet() { // 添加 jedis.sadd( "user" , "jerome" ); jedis.sadd( "user" , "jelly" ); jedis.sadd( "user" , "jack" ); jedis.sadd( "user" , "nemo" ); jedis.sadd( "user" , "who" ); // 移除noname jedis.srem( "user" , "who" ); System.out.println(jedis.smembers( "user" )); // 获取所有加入的value System.out.println(jedis.sismember( "user" , "who" )); // 判断 who是否是user集合的元素 System.out.println(jedis.srandmember( "user" )); System.out.println(jedis.scard( "user" )); // 返回集合的元素个数 } /** * redis排序 * * @throws InterruptedException */ public void testSort() throws InterruptedException { // 注意,此处的rpush和lpush是List的操作。是一个双向链表(但从表现来看的) jedis.del( "a" ); // 先清除数据,再加入数据进行测试 jedis.rpush( "a" , "1" ); jedis.lpush( "a" , "6" ); jedis.lpush( "a" , "3" ); jedis.lpush( "a" , "9" ); System.out.println(jedis.lrange( "a" , 0 , - 1 )); // [9, 3, 6, 1] System.out.println(jedis.sort( "a" )); // [1, 3, 6, 9] //输入排序后结果 System.out.println(jedis.lrange( "a" , 0 , - 1 )); } public static void main(String[] args) { TestRedis redis = new TestRedis(); redis.testMap(); // RedisUtil.getJedis().set("newname", "中文测试"); // System.out.println(RedisUtil.getJedis().get("newname")); } } |
使用连接池
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public final class RedisUtil { // Redis服务器IP private static String ADDR = "192.168.0.100" ; // Redis的端口号 private static int PORT = 6379 ; // 访问密码 // private static String AUTH = "admin"; // 可用连接实例的最大数目,默认值为8; // 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。 private static int MAX_ACTIVE = 1024 ; // 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。 private static int MAX_IDLE = 200 ; // 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException; private static int MAX_WAIT = 10000 ; private static int TIMEOUT = 10000 ; // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; private static boolean TEST_ON_BORROW = true ; private static JedisPool jedisPool = null ; /** * 初始化Redis连接池 */ static { try { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxActive(MAX_ACTIVE); config.setMaxIdle(MAX_IDLE); config.setMaxWait(MAX_WAIT); config.setTestOnBorrow(TEST_ON_BORROW); // jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH); jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT); } catch (Exception e) { e.printStackTrace(); } } /** * 获取Jedis实例 * * @return */ public synchronized static Jedis getJedis() { try { if (jedisPool != null ) { Jedis resource = jedisPool.getResource(); return resource; } else { return null ; } } catch (Exception e) { e.printStackTrace(); return null ; } } /** * 释放jedis资源 * * @param jedis */ public static void returnResource( final Jedis jedis) { if (jedis != null ) { jedisPool.returnResource(jedis); } } } |
code: 链接:http://pan.baidu.com/s/1qW5PJic 密码:pyha
参考