[spring-boot] 使用redis

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/crazyman2010/article/details/70903955

安装redis:

$ wget http://download.redis.io/releases/redis-3.2.8.tar.gz
$ tar xzf redis-3.2.8.tar.gz
$ cd redis-3.2.8
$ make

配置:

$ vi redis.conf
requirepass 123456  #配置密码
port 6231 #配置端口
daemonize yes #配置后台运行

启动:

$ ./src/redis-server redis.config

进入命令行:

$ ./src/redis-cli -p 6231 -a 123456

集成redis

在build.gradle中加入依赖:

compile 'org.springframework.boot:spring-boot-starter-data-redis'

在application.properties中加入配置信息:

#redis
spring.redis.host=localhost
spring.redis.password=123456
spring.redis.port=6231

没错,就是这么简单,就可以使用redis了

使用redis

@Autowired
private StringRedisTemplate redisTemplate; // <String,String>类型
//或者直接使用private RedisTemplate redisTemplate; 这样会注入<Object,Object>类型的,因为spring-boot-stater-data-redis默认创建了这样一个bean,不需要自己去配置了。

设置值:

ValueOperations<String, String> ops = redisTemplate.opsForValue();
ops.set(“key”,”value”);

获取值:

ops.get(“key”);

猜你喜欢

转载自blog.csdn.net/crazyman2010/article/details/70903955