Maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置文件
#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();
stringRedisTemplate.opsForList();
stringRedisTemplate.opsForSet();
stringRedisTemplate.opsForZSet();
示例
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;
public void redis(){
stringRedisTemplate.opsForValue().set("key", "value",60*5, TimeUnit.SECONDS);
stringRedisTemplate.boundValueOps("key").increment(-1);
stringRedisTemplate.opsForValue().get("key");
stringRedisTemplate.boundValueOps("key").increment(1);
stringRedisTemplate.getExpire("key");
stringRedisTemplate.getExpire("key",TimeUnit.SECONDS);
stringRedisTemplate.delete("key");
stringRedisTemplate.hasKey("key");
stringRedisTemplate.opsForSet().add("key", "1","2","3");
stringRedisTemplate.expire("key",1000 , TimeUnit.MILLISECONDS);
stringRedisTemplate.opsForSet().isMember("key", "1");
stringRedisTemplate.opsForSet().members("key");
}
}