一、概念:
Redis是一种基于键值对的NoSQL数据库,与很多键值对数据库不同的是,Redis中的值可以是String(字符串),hash(哈希),List(列表),set(集合),zset(有序集合)等多种数据结构;
二、特点:
1)高性能:
Redis将所有数据都存储在内存中,所以它的读写性能非常之高,官方的数据是可以达到10万/秒 ;
2)可靠性:
Redis还将内存中的数据利用快照和日志的形式保存到硬盘中,这样就可以避免发生断点或机器故障时,内存数据丢失的问题;
3)数据类型丰富:
字符串,hash,List,Set,Zset;
三、常用数据类型操作:
1、string:
//不局限于字符串,可以是json、xml,甚至是数字;
1)单值增删查:
SET key value
GET key
DEL key
2)多值增删查:
Mset key value [key value...]
Mget key [key...]
Del key [key]
)自增命令incr:
INCR key
DECR key
INCRBY key increment
DECRBY key decrement
4)java操作:
@Test
public void test(){
Jedis jedis = new Jedis("192.168.80.135", 6379);
jedis.auth("java1234");
jedis.set("name", "zs");
System.out.println(jedis.get("name"));
jedis.del("name");
jedis.mset("name","ls","age","12");
System.out.println(jedis.mget("name","age"));
jedis.del("name","age");
jedis.set("age", "12");
System.out.println(jedis.incr("age"));
System.out.println(jedis.incrBy("age", 100));
System.out.println(jedis.decr("age"));
System.out.println(jedis.decrBy("age",100));
}
2、Hash哈希:
//键中可以添加:冒号,没有逻辑作用,起语义化作用;
1)单值:
HSET key field value
HGET key field
HDEL key field [field ...]
2)多值:
HMSET key field value [field value ...]
HMGET key field [field ...]
HMDET key field [field ...]
3)其它:
)hlen key:计算个数;
)hexists key field:判断是否存在;
)hgetall key:获取所有键值对;
4)java操作:
@Test
public void test(){
Jedis jedis = new Jedis("192.168.80.135", 6379);
jedis.auth("java1234");
jedis.hset("user:1", "name", "zs");
System.out.println(jedis.hget("user:1", "name"));
jedis.hdel("user:1", "name");
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", "ls");
map.put("age", "12");
jedis.hmset("user:2", map);
System.out.println(jedis.hmget("user:2", "name", "age"));
System.out.println(jedis.hlen("user:2"));
System.out.println(jedis.hexists("user:2", "name"));
Map<String, String> stringStringMap = jedis.hgetAll("user:2");
Set<Map.Entry<String, String>> entries = stringStringMap.entrySet();
for (Iterator<Map.Entry<String, String>> iterator = entries.iterator(); iterator.hasNext(); ) {
Map.Entry<String, String> next = iterator.next();
System.out.println(next.getKey());
System.out.println(next.getValue());
}
jedis.hdel("user:2", "name", "age");
}
3、list列表:
//list列表类型是用来存储多个有序的字符串,且可重复,在开发中可以充当栈或队列的角色。
ps:栈:先进后出;队列:先进先出
1)进:
从左进:lpush key value [value...]
从右进:rpush key value [value...]
2)查:
向左出查看指定个数:lrange key startindex endindex;//指定出,endindex:-1表示末尾一个;
//没有向右出指定个数的指令;
3)出:
)向左边弹出:lpop key;
)向右边弹出:rpop key;
//每次出一个;
4)其它:
(1)llen key:计算个数;
)lindex key 0:获取key中索引是0的值;
)ltrim key 1 3:留下索引1到3范围内的值;
)lset key 1 a 把key中索引1位置的值设为a;
5)java操作:
@Test
public void test(){
Jedis jedis = new Jedis("192.168.80.135", 6379);
jedis.auth("java1234");
jedis.lpush("num", "1", "2", "3", "4");
jedis.rpush("num", "1", "2", "3", "4");
System.out.println(jedis.lrange("num", 0, -1));
while (jedis.llen("num")>0){
System.out.println(jedis.lpop("num"));
}
System.out.println("_____________");
jedis.lpush("num", "1", "2", "3", "4", "5", "6");
jedis.ltrim("num", 1, 3);
jedis.lset("num", 0, "10");
for (int i = 0; i < jedis.llen("num"); i++){
System.out.println(jedis.lindex("num", i));
}
}
4、Set集合:
//set集合不允许有重复的元素,并且集合中的元素是无序的。Redis支持对集合取交集,并集,差集。
1)增:
Sadd key element [element]:添加元素;
2)查:
Smembers key:获取所有的元素;
3)删:
Srem key element [element]:删除元素;
4)其它:
)Scard key:计算个数;
)Sismember key element:判断元素是否;
)Spop key:随机弹出一个元素;
5)集合间操作;
)Sinter key [key...]:求交集;
)Sunion key [key...]:求并集;
)Sdiff key [key...]:求差集;
4)保存结果:
- Sinterstore newkey key [key...]:保存交集的结果;
- Sunionstore newkey key [key...]:保存并集的结果;
- Sdiffstore newkey key [key...]:保存差集的结果;
5)java操作:
@Test
public void test() {
Jedis jedis = new Jedis("192.168.80.135", 6379);
jedis.auth("java1234");
jedis.sadd("set1", "1", "2", "3", "4", "5", "6");
System.out.println(jedis.sismember("set1", "1"));
while (jedis.scard("set1") > 0) {
System.out.println(jedis.spop("set1"));
}
System.out.println("____________");
jedis.sadd("set1", "1", "2", "3", "4", "5", "6");
jedis.sadd("set2", "4", "5", "6", "7", "8", "9");
System.out.println(jedis.sinter("set1", "set2"));
System.out.println(jedis.sunion("set1", "set2"));
System.out.println(jedis.sdiff("set1", "set2"));
System.out.println(jedis.sdiff("set2", "set1"));
}
5、zset集合:
//zset集合和set集合一样不允许有重复的元素,但集合中的元素是有序的。
1)增:
ZADD key score1 member1 [score2 member2]:添加元素,或更新已存在;
2)查:
ZRANGE key start stop [WITHSCORES]:获取所有的元素;
ZREVRANGE key start stop [WITHSCORES]:逆序获取;
3)删:
ZREM key member [member ...]:删除元素;
4)其它:
)ZCARD key:计算个数;
)ZSCORE key member:返回成员的分值;
)ZRANK key member:返回成员的索引;
)ZINCRBY key increment member:指定成员增加increment分;
5)java操作:
@Test
public void test() {
Jedis jedis = new Jedis("192.168.80.135", 6379);
jedis.auth("java1234");
Map<String, Double> map = new HashMap<String, Double>();
map.put("zs", 1.0);
map.put("ls", 2.0);
map.put("ww", 3.0);
jedis.zadd("user", map);
jedis.zincrby("user", 100.0, "ww");
Set<String> user = jedis.zrange("user", 0, -1);
for (Iterator<String> iterator = user.iterator(); iterator.hasNext(); ) {
String next = iterator.next();
System.out.println(next);
System.out.println(jedis.zscore("user", next));
}
}