篇十:Redis缓存
一、基础知识
下载学习:
官网:http://redis.io
中文网:http://www.redis.cn
归类:String/List/Set/Hash/Zset
Redis命令参考:http://redisdoc.com
学习点:
1、redis支持数据持久化
2、redis支持 key-value、list、set、zset、hash
3、redis支持数据备份(主从复制)
二、Linux部署Redis
安装步骤:
¬ 下载 redis-版本.tar.gz ( 例:redis-3.0.4.tar.gz )---> /opt目录
¬ cp redis-3.0.3.tar.gz /opt/
¬ cd /opt
¬ tar -xzvf redis-3.0.4.tar.gz
¬ cd redis-3.0.4
¬ yum install -y gcc-c++
¬ make
¬ make install
操作步骤:
¬ 配置文件:复制配置文件至 /etc
cd /usr/local/bin
cp redis.conf /etc/
¬ 启动服务:cd /usr/local/bin
redis-server /etc/redis.conf
¬ 启动客户端:cd /usr/local/bin
redis-cli –p 6379
¬ 校验服务:ping (校验客户端是否连接成功,如果输出 PONG,表示成功)
¬ 查看当前redis主从状态:info replication
¬ 关闭客户端:shutdown
¬ 关闭服务:exit
¬ 查看进程:ps –ef|grep redis
三、常用操作
数据库操作:
¬ 切换数据库:select 0 (select 下标)
¬ 值 key-value:set key value (set k1 v1)
¬ 取值 key-value:get ket (get k1 ,如果为空 ,打印 nil)
¬ 查询单库key数量:Dbsize
¬ 查询所有key: keys *
¬ key模糊查询:key k?
¬ 清空单库:flushdb
¬ 清空所有库:flushall
a、key:
¬ 判断key是否存在:exists key(例 exists k1)--(1:存在,0:不存在)
¬ key移库:move k2 2 (将key k2 移动到 2库)
¬ key设置过期时间:expire key 秒钟(例 expire k2 10,设置k2 10秒过期)
¬ 查看key存活时间:ttl key (-1表示永不过期,-2表示已过期)
¬ 查看key的类型:type key
¬ 删除key:del key
b、String(key-value):
¬ 创建:set key value
¬ 获取:get key
¬ 拼接:append key vvv(给key 的原始value尾部添加 vvv)
¬ 获取长度:strlen
¬ 数字自动+1:Incr key
¬ 数字自动-1:decr key
¬ 数字自动+n:incrby key n
¬ 数字自动-n:decrby key n
¬ 获取指定长度的String:getrange key start end
getrange key 0 -1:获取全部
getrange key 0 3:获取0-3
¬ 从指定位置插入:setrange key index xxxx
setrange key 1 abcd:在下标1 插入 abcd
¬ 创建key-value时指定过期时间:setex key 10 v4(key 存货10秒)
¬ 不存在创建:setnx key value(key 不存在时创建,避免覆盖)
¬ 批量创建:mset k1 v1 k2 v2 k3 v3
¬ 批量查询:mget k1 k2 k3
¬ 批量不存在则创建:msetnx k1 v1 k4 v4 k5 v5(有则不创建,没有就创建,相互不影响)
c、Hash(无序不重复,类似Java Map(String,Object):
¬ 注释:KV模式不变,但V是一个键值对
¬ hset/hget/hmset/hmget/hgetall/hdel
¬ 创建:hset user id 001
¬ 获取:hget user id
¬ 删除:hdel user id (删除V 的一个key-value)
¬ 批量创建:hmset user id 001 name lgp
¬ 批量获取:hmget user id name
¬ 批量获取:hgetall user(通过key获取V所有的键值对)
¬ 查询V的键值对数:hlen user
¬ V是否存在某个key:hexists user id (判断底是否存在)
¬ 获取V所有的key:hkeys user
¬ 获取V所有的value:hvals user
¬ V中value增长:hincrby user age 2 (年龄+2)
¬ V中value增长 :hincrbyfloat user score 0.5 (分数+0.5)
¬ key不存在则创建:hsetnx user name lgp
package com.guduo.common.utils.jedis; import java.util.Map; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * Jedis连接池 * @author liuguangping * */ public class PoolUtil { private PoolUtil() {} private static JedisPool readJedisPool = null; /** * 获取JedisPool的实例化--读 * @return */ public static JedisPool getJedisPoolInstance() { if(null == readJedisPool){ synchronized (PoolUtil.class) { if(null == readJedisPool){ JedisPoolConfig config = new PoolUtil().setJedisPoolConfig(); readJedisPool = new JedisPool(config,"192.168.1.52",6379); } } } return readJedisPool; } /** * 设置连接池参数 **/ private JedisPoolConfig setJedisPoolConfig(){ JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(1000); config.setMaxWaitMillis(100);//请求最短时间 config.setMaxIdle(32);// config.setTestOnBorrow(true); //设置检测连通性 return config; } /** * 释放Jedis * @param pool * @param jedis */ @SuppressWarnings("deprecation") public static void release(JedisPool pool,Jedis jedis){ if(null != jedis){ pool.returnResourceObject(jedis); } } /** * 获取jedis实例 * @return */ public static Jedis getJedis(){ Jedis jedis = getJedisPoolInstance().getResource(); return jedis; } /** * 存储Hash:指定存活时间 * @param key * @param map * @param times:存活时间(s) */ public static void insertHash(String key,Map<String, String> map,int times){ JedisPool pool = getJedisPoolInstance(); Jedis jedis = pool.getResource(); try { jedis.hmset(key, map); jedis.expire(key, times); } catch (Exception e) { e.printStackTrace(); }finally { release(pool, jedis); } } /** * 存储Hash:永久存在 * @param key * @param map */ public static void insertHash(String key,Map<String, String> map){ JedisPool pool = getJedisPoolInstance(); Jedis jedis = pool.getResource(); try { jedis.hmset(key, map); } catch (Exception e) { e.printStackTrace(); }finally { release(pool, jedis); } } /** * 存储String:指定存活时间 * @param key * @param value * @param times 存活时间(s) */ public static void insertString(String key,String value,int times){ JedisPool pool = getJedisPoolInstance(); Jedis jedis = pool.getResource(); try { jedis.setex(key, times, value); } catch (Exception e) { e.printStackTrace(); }finally { release(pool, jedis); } } /** * 存储String:默认永久存在 * @param key * @param value */ public static void insertString(String key,String value){ JedisPool pool = getJedisPoolInstance(); Jedis jedis = pool.getResource(); try { // jedis.setex(key, value); jedis.set(key, value); } catch (Exception e) { e.printStackTrace(); }finally { release(pool, jedis); } } /** * 批量存储String * @param key * @param map */ public static void batchInsertString(Map<String,Object> map){ JedisPool pool = getJedisPoolInstance(); Jedis jedis = pool.getResource(); try { for(String key:map.keySet()){ if(map.get(key).toString().equals("")) { jedis.set(key,""); }else{ jedis.set(key, map.get(key).toString()); } } } catch (Exception e) { e.printStackTrace(); }finally { release(pool, jedis); } } /** * 获取Hash * @param key * @return */ public static Map<String,String> getHash(String key){ Map<String,String> map = null; JedisPool pool = getJedisPoolInstance(); Jedis jedis = pool.getResource(); try { map = jedis.hgetAll(key); } catch (Exception e) { e.printStackTrace(); }finally { release(pool, jedis); } return map; } /** * 获取单个String * @param key * @return */ public static String getString(String key){ String value = null; JedisPool pool = getJedisPoolInstance(); Jedis jedis = pool.getResource(); try { value = jedis.get(key); } catch (Exception e) { e.printStackTrace(); }finally { release(pool, jedis); } return value; } /** * 判断key是否存在 * @param key * @return */ public static boolean checkIsExit(String key){ boolean isExit = false; JedisPool pool = getJedisPoolInstance(); Jedis jedis = pool.getResource(); try { isExit = jedis.exists(key); } catch (Exception e) { e.printStackTrace(); }finally { release(pool, jedis); } return isExit; } /** * 删除key * @param key */ public static void removeKey(String key){ JedisPool pool = getJedisPoolInstance(); Jedis jedis = pool.getResource(); try { jedis.del(key); } catch (Exception e) { e.printStackTrace(); } finally{ release(pool, jedis); } } }

浙公网安备 33010602011771号