1.引入倚赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-redis
spring-boot-starter-data-redis默认使用的工具是Lettuce,而是用jedis则倚赖如下
org.springframework.boot
spring-boot-starter-data-redis
io.lettuce
lettuce-core
redis.clients
jedis
org.springframework.boot
spring-boot-starter-web
#redis库的编号,默认是16个database,编号0~15号
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
#spring.redis.password=123@456
#连接池配置
#使用lettuce的配置
#spring.redis.lettuce.pool.max-active=
#spring.redis.lettuce.pool.max-idle=
#spring.redis.lettuce.pool.max-wait=
#spring.redis.lettuce.pool.min-idle=
#spring.redis.lettuce.shutdown-timeout=
#使用jedis的配置
#连接池最大连接数
spring.redis.jedis.pool.max-active=8
#连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1ms
#连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
package org.sang;
import java.io.Serializable;
/**
* Created by sang on 2018/7/18.
*/
public class Book implements Serializable {
private Integer id;
private String name;
private String author;
//省略getter/setter
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
4.测试
package org.sang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by sang on 2018/7/18.
*/
@RestController
public class BookController {
@Autowired
RedisTemplate redisTemplate;
/**
* StringRedisTemplate 是RedisTemplate的子类,StringRedisTemplate中的key和value都是字符串
* RedisTemplate则可以造作对象
*/
@Autowired
StringRedisTemplate stringRedisTemplate;
@GetMapping("/test1")
public void test1() {
ValueOperations ops1 = stringRedisTemplate.opsForValue();
ops1.set("name", "三国演义");
String name = ops1.get("name");
System.out.println(name);
ValueOperations ops2 = redisTemplate.opsForValue();
Book b1 = new Book();
b1.setId(1);
b1.setName("红楼梦");
b1.setAuthor("曹雪芹");
ops2.set("b1", b1);
Book book = (Book) ops2.get("b1");
System.out.println(book);
/*三国演义
Book{id=1, name='红楼梦', author='曹雪芹'}*/
}
}
1.倚赖中还要再添加
org.springframework.boot
spring-boot-starter-cache
配置文件中添加
#缓存配置
# 缓存名称,redis中key前缀为"缓存名::"
spring.cache.cache-names=c1,c2
#key的过期时间
spring.cache.redis.time-to-live=1800s
@SpringBootApplication
@EnableCaching
public class RediscacheApplication {
public static void main(String[] args) {
SpringApplication.run(RediscacheApplication.class, args);
}
}
package org.sang.rediscache;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
@Repository
public class BookDao {
@Cacheable("c1")
public Book getBookById(Integer id) {
System.out.println("getBookById");
Book book = new Book();
book.setId(id);
book.setName("三国演义");
book.setAuthor("罗贯中");
return book;
}
}
5.测试
package org.sang.rediscache;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
@Repository
public class BookDao {
@Cacheable("c1")
public Book getBookById(Integer id) {
System.out.println("getBookById");
Book book = new Book();
book.setId(id);
book.setName("三国演义");
book.setAuthor("罗贯中");
return book;
//Book{id=1, name='三国演义', author='罗贯中'}
//Book{id=2, name='三国演义', author='罗贯中'}
//Book{id=3, name='三国演义', author='罗贯中'}
}
}
wrong number of arguments for 'set' command
,后来发现是redis版本问题,本人使用的是win下最新的版本,后来更换其它版本后成功运行