当对String数据结构进行操作时,推荐直接使用spring-data-redis提供的StringRedisTemplate,其配置如下

<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean>

RedisTemplate使用ValueOperations对String类型进行基本操作。首先初始化spring工厂获得redisTemplate和opsForValue

private RedisTemplate<String,String> stringTemplate;
private ValueOperations<String, String> opsForValue; @SuppressWarnings("unchecked")
@Before
public void before(){
//初始化
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
stringTemplate = (RedisTemplate<String,String>)context.getBean("stringRedisTemplate");
opsForValue = stringTemplate.opsForValue();
}

下面使用Junit4测试工具进行演示,各位同学可以直接粘贴源代码自行测试使用,前提是你已经安装和配置好redis哦。

V get(Object key);
    @Test
public void testSet(){
//删除健(每次测试前我都会对当前测试的键进行删除,防止影响测试结果)
stringTemplate.delete("liu1");
opsForValue.set("liu1", "liu1");
System.out.println(opsForValue.get("liu1"));//liu1
}
void set(K key, V value, long timeout, TimeUnit unit);
    @Test
public void testSetTimeOut() throws InterruptedException{
stringTemplate.delete("liu2");
//加了失效机制
opsForValue.set("liu2", "liu2", 10, TimeUnit.SECONDS);
Thread.sleep(5000);
System.out.println(opsForValue.get("liu2"));//liu2
Thread.sleep(5000);
System.out.println(opsForValue.get("liu2"));//null
}
Boolean setIfAbsent(K key, V value);
  @Test
public void testSetIfAbsent(){
stringTemplate.delete("liu4");
stringTemplate.delete("liu5");
opsForValue.set("liu4", "liu4");
System.out.println(opsForValue.setIfAbsent("liu4", "liu4"));//false
System.out.println(opsForValue.setIfAbsent("liu5", "liu5"));//true
}
void multiSet(Map < ? extends K, ? extends V > map);
List multiGet(Collection < K > keys);
Boolean multiSetIfAbsent(Map< ? extends K, ? extends V > map);
    @Test
public void testMultiSetAndGet (){
stringTemplate.delete("liu6");
stringTemplate.delete("liu7");
stringTemplate.delete("liu8");
stringTemplate.delete("liu9");
Map<String,String> param = new HashMap<String,String>();
param.put("liu6", "liu6");
param.put("liu7", "liu7");
param.put("liu8", "liu8");
//为多个键分别设置它们的值
opsForValue.multiSet(param);
List<String> keys = new ArrayList<String>();
keys.add("liu6");
keys.add("liu7");
keys.add("liu8");
//为多个键分别取出它们的值
List<String> results = opsForValue.multiGet(keys);
for (String result : results) {
System.out.println(result);
/*
liu6
liu7
liu8
*/
}
param.clear();
param.put("liu8", "hahaha");
param.put("liu9", "liu9");
//为多个键分别设置它们的值,如果存在则返回false,不存在返回true
System.out.println(opsForValue.multiSetIfAbsent(param));//false
System.out.println(opsForValue.get("liu8"));//liu8
}
V getAndSet(K key, V value);
    @Test
public void testGetAndSet(){
stringTemplate.delete("liu9");
opsForValue.set("liu9", "liu9");
//设置键的字符串值并返回其旧值
System.out.println(opsForValue.getAndSet("liu9", "haha"));//liu9
System.out.println(opsForValue.get("liu9"));//haha
}
Long increment(K key, long delta);
Double increment(K key, double delta);
    @Test
public void testIncrement(){
stringTemplate.delete("liu10");
opsForValue.set("liu10", "6");
//值增长,支持整形和浮点型
System.out.println(opsForValue.increment("liu10", 1));//
System.out.println(opsForValue.increment("liu10", 1.1));//8.1
opsForValue.set("liu10", "liu10");
opsForValue.increment("liu10", 1);//redis.clients.jedis.exceptions.JedisDataException: ERR value is not an integer or out of range
}
Integer append(K key, String value);
    @Test
public void testAppend(){
stringTemplate.delete("liu11");
stringTemplate.delete("liu12");
//如果key已经存在并且是一个字符串,则该命令将该值追加到字符串的末尾。如果键不存在,则它被创建并设置为空字符串,因此APPEND在这种特殊情况下将类似于SET。
opsForValue.append("liu11", "liu11");
System.out.println(opsForValue.get("liu11"));//liu11
opsForValue.set("liu12", "liu12");
opsForValue.append("liu12", "haha");
System.out.println(opsForValue.get("liu12"));//liu12haha
}
String get(K key, long start, long end);
    @Test
public void testGetPart(){
stringTemplate.delete("liu13");
opsForValue.set("liu13", "liu13");
//截取key所对应的value字符串
System.out.println(opsForValue.get("liu13", 0, 2));//liu
}
void set(K key, V value);
    @Test
public void testSize(){
stringTemplate.delete("liu14");
opsForValue.set("liu14", "liu14");
//返回key所对应的value值得长度
System.out.println(opsForValue.size("liu14"));//
}
Boolean setBit(K key, long offset, boolean value);
   @Test
public void testSetBit(){
stringTemplate.delete("liu15");
//true为1,false为0
opsForValue.set("liu15", "liu15");
//对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit)
//key键对应的值value对应的ASCII码,在offset的位置(从左向右数)变为value
System.out.println(opsForValue.setBit("liu15", 13, true));//false
System.out.println(opsForValue.get("liu15"));//lmu15
for(int i = 0 ; i<"liu15".length()*8;i++){
if(opsForValue.getBit("liu15", i)){
System.out.print(1);
}else{
System.out.print(0);
}
//
}
}

转载自:https://blog.csdn.net/weixin_37490221/article/details/78134521

RedisTemplate访问Redis数据结构(一)——String的更多相关文章

  1. RedisTemplate访问Redis数据结构

    https://www.jianshu.com/p/7bf5dc61ca06 Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字 ...

  2. RedisTemplate访问Redis数据结构(介绍和常用命令)

    Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集 ...

  3. 如何使用RedisTemplate访问Redis数据结构之字符串操作

    Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集 ...

  4. 如何使用RedisTemplate访问Redis数据结构

    RedisTemplate介绍 spring封装了RedisTemplate对象来进行对redis的各种操作,它支持所有的 redis 原生的api. RedisTemplate在spring代码中的 ...

  5. Redis(九):使用RedisTemplate访问Redis数据结构API大全

    RedisTemplate介绍 spring封装了RedisTemplate对象来进行对redis的各种操作,它支持所有的 redis 原生的api. RedisTemplate在spring代码中的 ...

  6. 如何使用RedisTemplate访问Redis数据结构之list

    Redis的List数据结构 这边我们把RedisTemplate序列化方式改回之前的 Jackson2JsonRedisSerializer<Object> jackson2JsonRe ...

  7. RedisTemplate访问Redis数据结构(前言)

    Redis五种基本数据结构 redis提供键值对的形式对数据进行存储.支持五种数据类型:String(字符串),List(链表),Hash(散列),Set(无序集合),ZSet(有序集合).下面是网上 ...

  8. 如何使用RedisTemplate访问Redis数据结构之Zset

    Redis的ZSet数据结构 Redis 有序集合和无序集合一样也是string类型元素的集合,且不允许重复的成员. 不同的是每个元素都会关联一个double类型的分数.redis正是通过分数来为集合 ...

  9. RedisTemplate访问Redis数据结构(五)——ZSet

    Redis 有序集合和无序集合一样也是string类型元素的集合,且不允许重复的成员.不同的是每个元素都会关联一个double类型的分数.有序集合的成员是唯一的,但分数(score)却可以重复.red ...

随机推荐

  1. 【C语言--数据结构】线性表链式存储结构

    直接贴代码 头文件 #ifndef __LINKLIST_H__ #define __LINKLIST_H__ typedef void LinkList; typedef struct _tag_L ...

  2. HashMap底层为什么一定用数组

    HashMap源码数据结构: Entry[] table = new Entry[capacity]; 其中,Entry就是一个链表节点.如果将数组替换成LinkedList是否可行?如下: List ...

  3. Java——ArrayList底层源码分析

    1.简介 ArrayList 是最常用的 List 实现类,内部是通过数组实现的,它允许对元素进行快速随机访问.数组的缺点是每个元素之间不能有间隔, 当数组大小不满足时需要增加存储能力,就要将已经有数 ...

  4. MySQL索引,备份和还原

    1.索引  1.索引是占硬盘空间 ,也是按页存放的 . 思考题:一个索引页,(数据页)  占用多少个字节  .SQL Server 8192个字节 2.索引:是一种有效组合数据的方式,为了快速查找指定 ...

  5. POJ练习计划

    题目链接:https://cn.vjudge.net/article/348 2019/7/24: [POJ-1423] [题解] [POJ-1503] 模板题

  6. Windows 环境下安装redis 及其PHP Redis扩展

    1.安装Redis (1)这里选择在github官网上下载Redis,地址:Redis下载地址 下载压缩包(如下图),并解压到本地目录,我放在D:\redis (2)验证Redis安装是否成功打开命令 ...

  7. http协议是无状态协议,它的无状态指的是什么,如何解决这种情况

    http是无状态的协议,也是不安全的协议, 它的无状态是指对于事务处理没有记忆能力,缺少状态意味着后续的操作需要前面的信息. 解决办法:1,通过cookie解决,2,通过session会话保存.

  8. java字符流读取文件

    package ba; import java.io.*; public class zifuTest { public static void main(String[] args) { FileI ...

  9. Pose &&Get的区别

    从一个页面转向另一个页面的请求方式有两种,Post和Get. 如果从原理上来探究他们的区别,涉及到Http传输协议的细节,本文不加探究,只讨论一下表象. 1.Post传输数据时,不需要在URL中显示出 ...

  10. Docker其他操作:查看内部细节、IP、删除容器

    1.查看容器内部细节 查看容器运行内部细节,比如可看容器的IP docker inspect mycentos2 2.查看容器IP地址 直接显示IP地址 docker inspect --format ...