redis常见数据结构操作
ValueOperations对String数据结构进行操作
- void set(K key, V value);
{
redisTemplate.opsForValue().set("userName","ziwen")
redisTemplate.opsForValue().get("userName")
}
- void set(K key, V value, long timeout, TimeUnit unit);指定时间,多久之后过期
{
redisTemplate.opsForValue().set("userName","ziwen",10,TimeUnit.SECONDS)
redisTemplate.opsForValue().get("userName")
}
- void set(K key, V value, long offset);//从偏移量offset开始处覆写key对应的值.
{
redisTemplate.opsForValue().set("userName","ziwen");
redisTemplate.opsForValue().set("userName","ziwenwang",6);
}
- Boolean setIfAbsent(K key, V value);判断是否存在
{
template.opsForValue().setIfAbsent("ziwen","ziwen");//当返回 true 的时候,表示不存在.当返回 false的时候,表示存在
}
- multiSet void multiSet(Map
Redis的List数据结构
{
List<V> range(K key, long start, long end);返回存储在键中的列表的指定元素。偏移开始和停止是基于零的索引,其中0是列表的第一个元素(列表的头部),1是下一个元素
}
{
void trim(K key, long start, long end);
}
{
Long leftPush(K key, V value);
将所有指定的值插入存储在键的列表的头部。如果键不存在,则在执行推送操作之前将其创建为空列表。(从左边插入)
}
{
Long leftPushAll(K key, V... values);批量把一个数组插入到列表中
}
{
Long leftPushAll(K key, Collection<V> values);批量把一个集合插入到列表中
}
{
Long leftPushIfPresent(K key, V value);只有存在key对应的列表才能将这个value值插入到key所对应的列表中
}
{
Long remove(K key, long count, Object value);
从存储在键中的列表中删除等于值的元素的第一个计数事件。
计数参数以下列方式影响操作:
count> 0:删除等于从头到尾移动的值的元素。
count <0:删除等于从尾到头移动的值的元素。
count = 0:删除等于value的所有元素.
使用:System.out.println(template.opsForList().range("listRight",0,-1));
template.opsForList().remove("listRight",1,"setValue");
结果:[java, setValue, oc, c++]
[java, oc, c++]
}
{
V index(K key, long index);
}
{
V leftPop(K key);
}
{
V leftPop(K key, long timeout, TimeUnit unit);
移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
}
Redis的Hash数据机构
{
void put(H key, HK hashKey, HV value);设置散列hashKey的值
}
{
Map<HK, HV> entries(H key);
}
{
List<HV> values(H key)
tips:
redisTemplate.opsForHash().put("hash","name","ziwen")
redisTemplate.opsForHash().put("hash","pw","ziwen710")
redisTemplate.opsForHash().entries("hash")
//返回值为:{name=ziwen,pw=ziwen710}
//返回为:[ziwen,ziwen710],获取整个哈希存储的值根据密钥
}
{
Cursor<Map.Entry<HK, HV>> scan(H key, ScanOptions options)
使用Cursor在key的hash中迭代,相当于迭代器。
使用:Cursor<Map.Entry<Object, Object>> curosr = template.opsForHash().scan("redisHash", ScanOptions.ScanOptions.NONE)
while(curosr.hasNext()){
Map.Entry<Object, Object> entry = curosr.next()
System.out.println(entry.getKey()+":"+entry.getValue())
}
结果:age:28.1
class:6
kkk:kkk
}
{
Long delete(H key, Object... hashKeys);
}
{
Boolean hasKey(H key, Object hashKey);
}
{
HV get(H key, Object hashKey);
}
{
Long increment(H key, HK hashKey, long delta);
//通过给定的delta增加散列hashKey的值(整型)
}
{
Double increment(H key, HK hashKey, double delta);
}
{
Set<HK> keys(H key);
// 获取key所对应的散列表的key
}
Redis的Set数据结构
{
Long remove(K key, Object... values);
}
{
V pop(K key);
}
{
Set<V> members(K key);
// 返回集合中的所有成员
}
{
Cursor<V> scan(K key, ScanOptions options);
}
Redis的ZSet数据结构
{
Long add(K key, Set<TypedTuple<V>> tuples);
// 新增一个有序集合
}
{
Boolean add(K key, V value, double score);
}
{
Long remove(K key, Object... values);
}
{
Set<TypedTuple<V>> rangeWithScores(K key, long start, long end);
// 通过索引区间返回有序集合成指定区间内的成员对象,其中有序集成员按分数值递增(从小到大)顺序排列
}
{
Set<ZSetOperations.TypedTuple<Object>> tuples1=redisTemplate.opsForZSet().rangeWithScores("zset1",0,-1)
Iterator<ZSetOperations.TypedTuple<Object>> iterator= tuples1.iterator()
while (iterator.hasNext()){
ZSetOperations.TypedTuple<Object> typedTuple= iterator.next()
}
}