下载地址:https://github.com/MicrosoftArchive/redis/releases
下载后,解压,找到
点击运行:
在myeclipse中新建项目,导入jar包(jedis-2.1.0.jar,commons-pool-1.6.jar):
测试是否ping通:
package com.htxx.redis;
import redis.clients.jedis.Jedis;
public class Redis {
private Jedis jedis;//客户端连接
public Redis() {
// TODO Auto-generated constructor stub
//-------连接redis服务器 -----
jedis=new Jedis("localhost", 6379);
//-------查看服务是否运行,如果运行正常会返回PONG------
System.out.println("服务运行状况:"+jedis.ping());
//--------权限验证,输入密码admin--------
//jedis.auth("admin");
}
public static void main(String[] args) {
Redis redis=new Redis();
}
}
对String的操作:
public void testString(){
//--------设置key-value--------
jedis.set("name", "sasa");
System.out.println(jedis.get("name"));//sasa
jedis.append("name", " is very handsome");//在key为name 的值后面拼接" is very handsome"
System.out.println(jedis.get("name"));//sasa is very handsome
//--------设置多个键值对--------
String[] str={"gender","man","age","25"};
jedis.mset(str);
String[] keys={"name","gender","age"};
System.out.println(jedis.mget(keys));//[sasa is very handsome, man, 25]
//------删除某个键值对----
String[] key1={"age"};
jedis.del(key1);
//------判断某key是否存在-----------
System.out.println(jedis.exists("age"));//false
//------查看某个key的剩余生存时间,永久生存
返回值为-1
或者不存在的Key
返回值为-2
,------------- System.out.println(jedis.ttl("name"));//-1 //-----清空数据库------- System.out.println(jedis.flushDB());//OK //------判断某key是否存在------------------- System.out.println(jedis.exists("name"));//false
//设置某一键值对的有效期
expire a 10 //-------设置a的有效期为10秒--------
}
对Map的操作:
public void testMap(){
Map map=new HashMap();
map.put("name", "sasa");
map.put("gender", "man");
map.put("looks", "handsome");
//-------同时将多个 field-value (域-值)对设置到哈希表 key 中------
jedis.hmset("user", map);
//-------返回哈希表 key 中,一个或多个给定域的值---------
List values=jedis.hmget("user", "name","gender");
System.out.println(values);//[sasa, man]
//-----------删除map中某一个键值对----------
jedis.hdel("user", "looks");
//-----------查看是否还存在looks的键值对-------
System.out.println(jedis.hmget("user", "looks"));//[null]
//----------查看名称为key的hash中元素个数------
System.out.println(jedis.hlen("user"));//2
//--------是否存在key为user的记录 返回true--------
System.out.println(jedis.exists("user"));//true
//---------是否存在key为name的记录 返回true--------
System.out.println(jedis.exists("name"));//false
//---------返回map对象中所有key-------------
Set keys=jedis.hkeys("user");
System.out.println(keys);//[gender, name]
//--------返回map对象中所有value------------
List vals=jedis.hvals("user");
System.out.println(vals);//[man, sasa]
}
对List操作:
public void testList(){
//----------清空数据---------
jedis.flushDB();
//-------增加数据---------------
jedis.lpush("name", "zhangsan");
jedis.lpush("name", "lisi");
jedis.lpush("name", "wangwu");
jedis.lpush("name", "zhaoliu");
//---------按范围取出数据,第一个是想取出值的key,第二个是起点,第三个是终点--------------
List list=jedis.lrange("name",0, jedis.llen("name"));
System.out.println(list);//[zhaoliu, wangwu, lisi, zhangsan]
list=jedis.lrange("name",0,-1);
System.out.println(list);//[zhaoliu, wangwu, lisi, zhangsan]
list=jedis.lrange("name",0, 1);
System.out.println(list);//[zhaoliu, wangwu]
//--------------在右侧添加某个值------------
jedis.rpush("name","zhaoliu");
System.out.println(jedis.lrange("name",0,-1));//[zhaoliu, wangwu, lisi, zhangsan, zhaoliu]
//-------------移除某个key尾部值--------------------
jedis.rpop("name");
System.out.println(jedis.lrange("name",0,-1));//[zhaoliu, wangwu, lisi, zhangsan]
//-------------删除key-----------
jedis.del("name");
System.out.println(jedis.exists("name"));//fasle
}
对LIST操作,分左和右(头和尾)
public void testSet(){
//jedis.flushDB();
jedis.sadd("pet", "pig","dog","cat","mouse");
//----------------查看pet集合中所有元素-----------
System.out.println(jedis.smembers("pet"));//[mouse, cat, dog, pig]
//----------------pet集合删除元素---------------
jedis.srem("pet", "dog");
System.out.println(jedis.smembers("pet"));//[mouse, cat, pig]
//----------------判断某一元素是否在集合内------------
System.out.println(jedis.sismember("pet", "dog"));//false
//---------------集合内元素个数----------------------
System.out.println(jedis.scard("pet"));//3
}
public void testKey(){
//-------------判断某个key是否存在------------
System.out.println(jedis.exists("name"));//false
//-------------添加某个key-------------
jedis.set("name", "sasa");
jedis.set("gender", "man");
//---------------再次测试某个key是否存在---------
System.out.println(jedis.exists("name"));//true
//----------------查看所有key----------------
Set set=jedis.keys("*");
System.out.println(set);//[gender, name, pet]
//---------------删除某个key,若key不存在,忽略该命令-----
jedis.del("pet");
System.out.println(jedis.keys("*"));//[gender, name]
//设置过期时间
jedis.expire("gender", 2);
try{
Thread.sleep(3000);
}catch(Exception e){
e.printStackTrace();
}
System.out.println(jedis.keys("*"));//[name]
}
参考:
http://www.cnblogs.com/liuling/p/2014-4-19-04.html
http://www.cnblogs.com/edisonfeng/p/3571870.html
http://redisdoc.com/index.html 里面有关于redis命令详解