当对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

最新文章

  1. python 环境配置
  2. ios 模拟器内部网络连接问题
  3. rar 命令
  4. 验证码的种类与实现 C#封装类 - .NET MVC WEBFORM
  5. dubbo学习之服务消费者
  6. 转:python字符串/元组/列表/字典互转
  7. Swift之基础知识
  8. 谷歌官方刷新组件SwipeRefreshLayout
  9. 开源安全测试 - BackTrack的应用
  10. Android的Touch系统简介(一)
  11. 泛泰A870S官方4.4.2系统S0218210 内核版本号信息
  12. hdu-4833-Best-Financing(DP)
  13. ng指令控制一个元素的影藏的与显示几种方法的使用
  14. 新版MATERIAL DESIGN 官方动效指南(一)
  15. Nginx 怎么给一台服务器,配置两个域名?详细的解说+截图教程
  16. grep,sed,awk用法整理
  17. 04、SQL 查询当天,本月,本周的记录
  18. JSON库的使用研究(二)
  19. hibernate update 只更新部分字段的3种方法(转载)
  20. oracle执行update时卡死问题的解决办法

热门文章

  1. selenium学习-对当前浏览器窗口截屏
  2. linux文件属性软硬连接
  3. JGit、SvnKit - 版本提交日志(1)提取
  4. 2019年安徽大学ACM/ICPC实验室新生赛(公开赛)D 不定方程
  5. 网络 osi七层协议
  6. 剑指offer-两个链表的第一个公共结点-链表-python
  7. django学习笔记(四)
  8. FTP连接不上的解决方法
  9. js验证小数或者整数
  10. listalias - 列出用户和系统别名