#download
? ? ? 1)鏈接:http://pan.baidu.com/s/1sk0HzrB?
? ? ? 2)密碼:adjw
#命令行運(yùn)行(win8.1為例)
? ? ? 1)進(jìn)入解壓目錄?
? ? ? ?#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服務(wù):
src/redis-cli shutdown
增刪改查:
keys *
取出當(dāng)前匹配的所有key
> exists larry
(integer) 0
當(dāng)前的key是否存在
del lv
刪除當(dāng)前key
expire
設(shè)置過期時(shí)間
> expire larry 10
(integer) 1
> move larry ad4
(integer) 1
移動(dòng)larry鍵值對(duì)到ad4數(shù)據(jù)庫
> persist lv
(integer) 1
移除當(dāng)前key的過期時(shí)間
randomkey
隨機(jī)返回一個(gè)key
rename
重命名key
type
返回值的數(shù)據(jù)類型
type testlist
list
> ping
PONG
測試連接是否還在
>echo name
"larry"
打印
> select ad4databank
OK
數(shù)據(jù)庫切換
> quit
退出連接
> dbsize
(integer) 12
當(dāng)前數(shù)據(jù)庫中key的數(shù)量
> info
服務(wù)器基本信息
monitor
實(shí)時(shí)轉(zhuǎn)儲(chǔ)收到的請(qǐng)求
config get
獲取服務(wù)器的參數(shù)配置
flushdb
清空當(dāng)前數(shù)據(jù)庫
flushall
清除所有數(shù)據(jù)庫
#相關(guān)代碼
<dependency>
? ? ? ? <groupId>redis.clients</groupId>
? ? ? ? <artifactId>jedis</artifactId>
? ? ? ? <version>2.6.2</version>
</dependency>
//構(gòu)建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;
}
//獲取數(shù)據(jù)
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對(duì)象
? ? ? ? ? pool.returnBrokenResource(jedis);
? ? ? ? ?e.printStackTrace();
? ? }finally{
? ? ? ? ? //返還到連接池
? ? ? ? ?returnResource(pool, jedis);
? ? ?}
? ? ?returnvalue;
}
//儲(chǔ)存數(shù)據(jù)
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對(duì)象
? ? ? ? ? ? ? ?pool.returnBrokenResource(jedis);
? ? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? ?}finally{
? ? ? ? ? ? ? //返還到連接池
? ? ? ? ? ? ? returnResource(pool, jedis);
? ? ? ? ?}
}
//刪除數(shù)據(jù)
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對(duì)象
? ? ? ? ? ? ? ? ?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對(duì)象
? ? ? ? ? ? ? ? pool.returnBrokenResource(jedis);
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }finally{
? ? ? ? ? ? ? ? ?//返還到連接池
? ? ? ? ? ? ? ? ?returnResource(pool, jedis);
? ? ? ? ? ? }
}
//返還到連接池
public static void returnResource(JedisPool pool, Jedis redis) {
? ? ? ? ? ? if(redis !=null) {
? ? ? ? ? ? ? ? ? pool.returnResource(redis);
? ? ? ? ? ? }
}