redis应用

redis应用

关于redis的安装及学习有很完整的文档,http://www.runoob.com/redis/redis-tutorial.html,这里主要是讲spring-boot应用redis

1.依赖

            <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.3.RELEASE</version>
</dependency>

2.配置

redis.host=127.0.0.1
redis.port=6379
redis.timeout=10000
redis.password=test
redis.maxIdle=1000
redis.minIdle=8
redis.testOnBorrow=true

redis.testOnReturn=true

3.RedisOperate

@Service
public class RedisOperate {


@Resource(name="redisTemplate")
public RedisTemplate<String, Object> redisTemplate;


@Resource(name="redisTemplate")
protected RedisTemplate<Serializable, Serializable> redisTemplateSerializable;


/**
 * 缓存基本的对象,Integer、String、实体类等
 * 
 * @param key
 *            缓存的键值
 * @param value
 *            缓存的值
 * @return 缓存的对象
 */
public void setCacheObject(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}


/**
 * 获得缓存的基本对象。
 * 
 * @param key
 *            缓存键值
 * @param operation
 * @return 缓存键值对应的数据
 */
public Object getCacheObject(
String key/* ,ValueOperations<String,T> operation */) {
return redisTemplate.opsForValue().get(key);
}


/**
 * 缓存List数据
 * 
 * @param key
 *            缓存的键值
 * @param dataList
 *            待缓存的List数据
 * @return 缓存的对象
 */

public <T> ListOperations<String, T> setCacheList(String key,List<T> dataList)
    {
        ListOperations<String, Object> listOperation = redisTemplate.opsForList();
        if(null != dataList)
        {
            int size = dataList.size();
            for(int i = 0; i < size ; i ++)
            {                
                listOperation.rightPush(key,dataList.get(i));
            }
        }        
        return null;
    }


/**
 * 获得缓存的list对象
 * 
 * @param key
 *            缓存的键值
 * @return 缓存键值对应的数据
 */
public <T> List<T> getCacheList(String key)
    {
        List<T> dataList = new ArrayList<T>();
        ListOperations<String,T> listOperation = (ListOperations<String, T>) redisTemplate.opsForList();
        Long size = listOperation.size(key);
        
        for(int i = 0 ; i < size ; i ++)
        {
            dataList.add((T) listOperation.leftPop(key));
        }
        
        return dataList;
    }


/**
 * 获得缓存的list对象 @Title: range @Description: end @param @return @return List
 * <T> 返回类型 @throws
 */
public List<Object> range(String key, long start, long end) {
ListOperations<String, Object> listOperation = redisTemplate.opsForList();
return listOperation.range(key, start, end);
}


/**
 * list集合长度
 * 
 * @param key
 * @return
 */
public Long listSize(String key) {
return redisTemplate.opsForList().size(key);
}


/**
 * 覆盖操作,将覆盖List中指定位置的值
 * 
 * @param key
 * @param int
 *            index 位置
 * @param String
 *            value 值
 * @return 状态码
 */
public void listSet(String key, int index, Object obj) {
redisTemplate.opsForList().set(key, index, obj);
}


/**
 * 向List尾部追加记录
 * 
 * @param String
 *            key
 * @param String
 *            value
 * @return 记录总数
 */
public long leftPush(String key, Object obj) {
return redisTemplate.opsForList().leftPush(key, obj);
}


/**
 * 向List头部追加记录
 * 
 * @param String
 *            key
 * @param String
 *            value
 * @return 记录总数
 */
public long rightPush(String key, Object obj) {
return redisTemplate.opsForList().rightPush(key, obj);
}


/**
 * 算是删除吧,只保留start与end之间的记录
 * 
 * @param String
 *            key
 * @param int
 *            start 记录的开始位置(0表示第一条记录)
 * @param int
 *            end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)
 * @return 执行状态码
 */
public void trim(String key, int start, int end) {
redisTemplate.opsForList().trim(key, start, end);
}


/**
 * 删除List中c条记录,被删除的记录值为value
 * 
 * @param String
 *            key
 * @param int
 *            c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录
 * @param Object
 *            obj 要匹配的值
 * @return 删除后的List中的记录数
 */
public long remove(String key, long i, Object obj) {
return redisTemplate.opsForList().remove(key, i, obj);
}


/**
 * 缓存Set
 * 
 * @param key
 *            缓存键值
 * @param dataSet
 *            缓存的数据
 * @return 缓存数据的对象
 */
public BoundSetOperations<String, Object> setCacheSet(String key, Set<Object> dataSet) {
BoundSetOperations<String, Object> setOperation = redisTemplate.boundSetOps(key);
/*
 * T[] t = (T[]) dataSet.toArray(); setOperation.add(t);
 */


Iterator<Object> it = dataSet.iterator();
while (it.hasNext()) {
setOperation.add(it.next());
}
return setOperation;
}


/**
 * 获得缓存的set
 * 
 * @param key
 * @param operation
 * @return
 */
public Set<Object> getCacheSet(
String key/* ,BoundSetOperations<String,T> operation */) {
Set<Object> dataSet = new HashSet<Object>();
BoundSetOperations<String, Object> operation = redisTemplate.boundSetOps(key);


Long size = operation.size();
for (int i = 0; i < size; i++) {
dataSet.add(operation.pop());
}
return dataSet;
}


/**
 * 缓存Map
 * 
 * @param key
 * @param dataMap
 * @return
 */
public int setCacheMap(String key, Map<String, Object> dataMap) {
if (null != dataMap) {
HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
/*
 * System.out.println("Key = " + entry.getKey() + ", Value = " +
 * entry.getValue());
 */
if (hashOperations != null) {
hashOperations.put(key, entry.getKey(), entry.getValue());
} else {
return 0;
}
}
} else {
return 0;
}
return dataMap.size();
}


/**
 * 获得缓存的Map
 * 
 * @param key
 * @param hashOperation
 * @return
 */
public Map<Object, Object> getCacheMap(
String key/* ,HashOperations<String,String,T> hashOperation */) {
Map<Object, Object> map = redisTemplate.opsForHash().entries(key);
/* Map<String, T> map = hashOperation.entries(key); */
return map;
}


/**
 * 缓存Map
 * 
 * @param key
 * @param dataMap
 * @return
 */
public void setCacheIntegerMap(String key, Map<Integer, Object> dataMap) {
HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
if (null != dataMap) {
for (Map.Entry<Integer, Object> entry : dataMap.entrySet()) {
/*
 * System.out.println("Key = " + entry.getKey() + ", Value = " +
 * entry.getValue());
 */
hashOperations.put(key, entry.getKey(), entry.getValue());
}


}
}


/**
 * 获得缓存的Map
 * 
 * @param key
 * @param hashOperation
 * @return
 */
public Map<Object, Object> getCacheIntegerMap(
String key/* ,HashOperations<String,String,T> hashOperation */) {
Map<Object, Object> map = redisTemplate.opsForHash().entries(key);
/* Map<String, T> map = hashOperation.entries(key); */
return map;
}


/**
 * 从hash中删除指定的存储
 * 
 * @param String
 * @return 状态码,1成功,0失败
 */
public void deleteMap(String key) {
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.opsForHash().delete(key);
return;
}


/**
 * 设置过期时间
 * 
 * @param key
 * @param time
 * @param unit
 * @return
 */
public boolean expire(String key, long time, TimeUnit unit) {
return redisTemplate.expire(key, time, unit);
}


/**
 * increment
 * 
 * @param key
 * @param step
 * @return
 */
public long increment(String key, long step) {
return redisTemplate.opsForValue().increment(key, step);
}


// redisTemplateSerializable


/**
 * 删除redis的所有数据
 */
/*
 * @SuppressWarnings({"unchecked", "rawtypes"}) public String flushDB() {
 * return redisTemplateSerializable.execute(new RedisCallback() { public
 * String doInRedis(RedisConnection connection) throws DataAccessException {
 * connection.flushDb(); return "ok"; } }); }
 */


public long del(final byte[] key) {
return (long) redisTemplateSerializable.execute(new RedisCallback<Object>() {
@Override
public Long doInRedis(RedisConnection connection) {
return connection.del(key);
}
});
}


@SuppressWarnings({ "unchecked", "rawtypes" })
public byte[] get(final byte[] key) {
return (byte[]) redisTemplateSerializable.execute(new RedisCallback() {
@Override
public byte[] doInRedis(RedisConnection connection) throws DataAccessException {
return connection.get(key);
}
});
}


/**
 * @param key
 * @param value
 * @param liveTime
 */
public void set(final byte[] key, final byte[] value, final long liveTime) {
redisTemplateSerializable.execute(new RedisCallback<Object>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
connection.set(key, value);
if (liveTime > 0) {
connection.expire(key, liveTime);
}
return 1L;
}
});
}
}

4.作用

Redis是一个开源的key—value型数据库,支持string、list、set、zset和hash类型数据。对这些数据的操作都是原子性的,redus为了保证效率会定期持久化数据。

详细学习 http://www.runoob.com/redis/redis-tutorial.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值