redistemplate文档用法_SpringBoot整合Redis及StringRedisTemplate的使用

Maven依赖

org.springframework.boot

spring-boot-starter-data-redis

配置文件

#redis的ip地址

spring.redis.hostName=127.0.0.1

#数据库,默认为0

spring.redis.database=0

#端口号

spring.redis.port=6379

#如果有密码

spring.redis.password=

#客户端超时时间单位是毫秒 默认是2000

spring.redis.timeout=10000

StringRedisTemplate使用

stringRedisTemplate.opsForValue();//操作字符串

stringRedisTemplate.opsForHash();//操作hash

stringRedisTemplate.opsForList();//操作list

stringRedisTemplate.opsForSet();//操作set

stringRedisTemplate.opsForZSet();//操作有序set

示例

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service

public class RedisService {

@Autowired

StringRedisTemplate stringRedisTemplate;

/**

* stringRedisTemplate基本操作

*/

public void redis(){

stringRedisTemplate.opsForValue().set("key", "value",60*5, TimeUnit.SECONDS);//向redis里存入数据和设置缓存时间(5分钟)

stringRedisTemplate.boundValueOps("key").increment(-1);//val做-1操作

stringRedisTemplate.opsForValue().get("key");//根据key获取缓存中的val

stringRedisTemplate.boundValueOps("key").increment(1);//val +1

stringRedisTemplate.getExpire("key");//根据key获取过期时间

stringRedisTemplate.getExpire("key",TimeUnit.SECONDS);//根据key获取过期时间并换算成指定单位

stringRedisTemplate.delete("key");//根据key删除缓存

stringRedisTemplate.hasKey("key");//检查key是否存在,返回boolean值

stringRedisTemplate.opsForSet().add("key", "1","2","3");//向指定key中存放set集合

stringRedisTemplate.expire("key",1000 , TimeUnit.MILLISECONDS);//设置过期时间

stringRedisTemplate.opsForSet().isMember("key", "1");//根据key查看集合中是否存在指定数据

stringRedisTemplate.opsForSet().members("key");//根据key获取set集合

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值