连接:redis-cli
Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。
常用key value命令查看排序值 ZRANGE key 0 1000 WITHSCORES
五:Redis HyperLogLog
用于统计数据数量count用
添加 pfadd key value
查看数量 pfcount key
六:redis发布订阅
实现监听:subscribe 通道名
发送数据给其他监听客户端 publish 通道名 字符串数据
七:redis事务
开始事务 multi
结束事务 exec 所有中间执行结果在结束事务后打印出来
八:redis脚本
EVAL script numkeys key [key ...] arg [arg ...]
redis 127.0.0.1:6379> EVAL "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1 key2 first second 1) "key1" 2) "key2" 3) "first" 4) "second"其他与java连接操作
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;
public class test {
public static void main(String[] args) throws Exception {
// Connecting to Redis server on localhost
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server sucessfully");
// check whether server is running or not
System.out.println("Server is running: " + jedis.ping());
Map map = new HashMap();
map.put("key1", "123");
map.put("key2", "456");
map.put("key3", "789");
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(map);
byte[] bytes = bo.toByteArray();
bo.close();
oo.close();
jedis.set("测试set".getBytes(), bytes);
byte[] bs = jedis.get("测试set".getBytes());
ByteArrayInputStream read = new ByteArrayInputStream(bs);
ObjectInputStream in = new ObjectInputStream(read);
Object readObject = in.readObject();
System.out.println("测试set添加byte:"+readObject); //key-value一对一
jedis.lpush("tutorial-list", "Redis");
jedis.lpush("tutorial-list", "Mongodb");
jedis.lpush("tutorial-list", "Mongodb");
jedis.lpush("tutorial-list", "Mysql");
List list = jedis.lrange("tutorial-list", 0 ,5);
System.out.println("测试lpush添加:"+list); //value值可以重复
jedis.sadd("测试sadd", "123");
jedis.sadd("测试sadd", "456");
jedis.sadd("测试sadd", "123");
jedis.sadd("测试sadd", "789");
Set smembers = jedis.smembers("测试sadd");
System.out.println("测试sadd添加:"+smembers); //value值不可以重复
jedis.zadd("测试zadd", 3, "123");
jedis.zadd("测试zadd", 1, "123");
jedis.zadd("测试zadd", 2, "456");
jedis.zadd("测试zadd", 4, "789");
Set zrange = jedis.zrange("测试zadd", 0, 10);
System.out.println("测试zadd添加zrange输出:"+zrange); //value值不可以重复
Set zrangeByScore = jedis.zrangeByScore("测试zadd", 0, 10);
System.out.println("测试zadd添加zrangeByScore输出:"+zrangeByScore);
Set zrangeWithScores = jedis.zrangeWithScores("测试zadd", 0, 10);
System.out.println("测试zadd添加zrangeWithScores输出:"+zrangeWithScores);
jedis.pfadd("测试pfadd", "123");
jedis.pfadd("测试pfadd", "456");
jedis.pfadd("测试pfadd", "789");
jedis.pfadd("测试pfadd", "123");
long pfcount = jedis.pfcount("测试pfadd");
System.out.println("测试pfadd返回value数量:"+pfcount); //value值不可以重复
Set keys = jedis.keys("*");
System.out.println("所有的key:"+keys);
Long del = jedis.del("测试set");
System.out.println("删除key返回数量:"+del);
Object eval = jedis.eval("return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", 2, "key1","key2","value1","value2");
System.out.println("测试eval:"+eval);
jedis.close();
}
}
输出结果:
Connection to server sucessfully
Server is running: PONG
测试set添加byte:{key3=789, key2=456, key1=123}
测试lpush添加:[Mysql, Mongodb, Mongodb, Redis]
测试sadd添加:[123, 456, 789]
测试zadd添加zrange输出:[123, 456, 789]
测试zadd添加zrangeByScore输出:[123, 456, 789]
测试zadd添加zrangeWithScores输出:[[[49, 50, 51],1.0], [[52, 53, 54],2.0], [[55, 56, 57],4.0]]
测试pfadd返回value数量:3
所有的key:[测试sadd, tutorial-list, 测试zadd, 测试set, 测试pfadd]
删除key返回数量:1
测试eval:[key1, key2, value1, value2]
重启redis,保存在内存中的所有数据消失