#download
1)链接:http://pan.baidu.com/s/1sk0HzrB
2)密码:adjw
#命令行运行(win8.1为例)
1)进入解压目录
#cd/....
redis-server.exe redis.conf
redis-cli.exe -h 127.0.0.1 -p 6379
#常用指令
set lu jianing
get lu
src/redis-cli
停止redis服务:
src/redis-cli shutdown
增删改查:
keys *
取出当前匹配的所有key
> exists larry
(integer) 0
当前的key是否存在
del lv
删除当前key
expire
设置过期时间
> expire larry 10
(integer) 1
> move larry ad4
(integer) 1
移动larry键值对到ad4数据库
> persist lv
(integer) 1
移除当前key的过期时间
randomkey
随机返回一个key
rename
重命名key
type
返回值的数据类型
type testlist
list
> ping
PONG
测试连接是否还在
>echo name
"larry"
打印
> select ad4databank
OK
数据库切换
> quit
退出连接
> dbsize
(integer) 12
当前数据库中key的数量
> info
服务器基本信息
monitor
实时转储收到的请求
config get
获取服务器的参数配置
flushdb
清空当前数据库
flushall
清除所有数据库
#相关代码
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.2</version>
</dependency>
//构建redis连接池
private staticJedisPool pool=null;
public staticJedisPool getPool() {
if(pool==null) {
JedisPoolConfig config =newJedisPoolConfig();
config.setTestOnBorrow(true);
pool=newJedisPool(config,"127.0.0.1",6379,2000);
}
returnpool;
}
//获取数据
public staticObject get(String key) {
Object value =null;
JedisPool pool =null;
Jedis jedis =null;
try{
pool =getPool();
jedis = pool.getResource();
byte[] obj = jedis.get(key.getBytes());
value = SerializeUtil.unserialize(obj);
}catch(Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
}finally{
//返还到连接池
returnResource(pool, jedis);
}
returnvalue;
}
//储存数据
public static void set(String key, Object value) {
JedisPool pool =null;
Jedis jedis =null;
try{
pool =getPool();
jedis = pool.getResource();
jedis.set(key.getBytes(), SerializeUtil.serialize(value));
}catch(Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
}finally{
//返还到连接池
returnResource(pool, jedis);
}
}
//删除数据
public static void del(String key) {
JedisPool pool =null;
Jedis jedis =null;
try{
pool =getPool();
jedis = pool.getResource();
jedis.del(key.getBytes());
}catch(Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
}finally{
//返还到连接池
returnResource(pool, jedis);
}
}
//清空
public static void cleanAll() {
JedisPool pool =null;
Jedis jedis =null;
try{
pool =getPool();
jedis = pool.getResource();
jedis.flushAll();
System.out.println("清理完毕.....");
}catch(Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
}finally{
//返还到连接池
returnResource(pool, jedis);
}
}
//返还到连接池
public static void returnResource(JedisPool pool, Jedis redis) {
if(redis !=null) {
pool.returnResource(redis);
}
}