Redis基础

Redis是开源的,支持网络、可基于内存也可持久化的key-value数据库,支持主从复制,读写分离,支持5种基本数据类型,高并发性能

安装redis数据库

下载:http://redis.io/download

tar zxvf redis-2.1.0

make (编译Redis)

make install (安装)

redis-server my.conf(启动服务)

redis-cli(打开控制台)

与Java集成,引入jar包如下:

Redis基础

JAVA-API示例:

public class RedisTest{

private Jedis jedis = null; 

    RedisTest() {
//连接redis服务
jedis = new Jedis("127.0.0.1", 6379);
//jedis = new Jedis("localhost"); //权限认证
//jedis.auth("admin"); //查看服务是否运行
System.out.println("Server is running: "+jedis.ping());
} public void testString() {
//添加
jedis.set("name","luangeng"); //拼接
jedis.append("name", " is good"); //删除某个键
jedis.del("name"); //设置多个键值对
jedis.mset("name","luangeng","age","123","qq","1234"); //增1
jedis.incr("age");
} public void testList() {
//Remove the specified keys. returns the number of keys removed.
jedis.del("list","key2","key3"); // 从左边加入
jedis.lpush("list", "5");
// 从右边加入
jedis.rpush("list", "0"); jedis.rpush("list", "-1"); jedis.rpush("list", "2"); long n = jedis.llen("list"); //Sort a Set or a List and Store the Result at dstkey.
jedis.sort("list","b");
System.out.println(jedis.lrange("list",0,-1));
System.out.println(jedis.lrange("b",0,-1));
} public void testSet(){
jedis.del("set");
//添加
jedis.sadd("set","123");
jedis.sadd("set","123");
jedis.sadd("set","1");
jedis.sadd("set","1");
jedis.sadd("set","2");
//移除
jedis.srem("set","2"); System.out.println(jedis.scard("set"));
System.out.println(jedis.smembers("set"));
System.out.println(jedis.sismember("set", "123"));
} public void testMap() {
Map<String, String> map = new HashMap<String, String>();
map.put("k1", "v1");
map.put("k2", "v2");
map.put("k3", "v3");
jedis.hmset("map",map); List<String> rsmap = jedis.hmget("map", "k1", "k2", "k3");
System.out.println(rsmap); jedis.hdel("k3","v3"); Iterator<String> iter=jedis.hkeys("map").iterator();
while (iter.hasNext()){
String key = iter.next();
System.out.println(key+":"+jedis.hmget("map",key));
} jedis.hlen("map");
jedis.exists("map");
jedis.hkeys("map");
jedis.hvals("map");
} public void flushDB(){
//Delete all the keys of the currently selected DB. This command never fails.
jedis.flushDB(); //Delete all the keys of all the existing databases.This command never fails.
//jedis.flushAll();
} public static void main(String[] args) throws InterruptedException { RedisTest rtest = new RedisTest();
rtest.testString();
rtest.testList();
rtest.testSet();
rtest.testMap();
rtest.flushDB(); } }

  

上一篇:IsBadStringPtr、IsBadWritePtr


下一篇:Word2007怎样从随意页開始设置页码 word07页码设置毕业论文