SpringBoot与RedisTemplate
SpringBoot与RedisTemplate
Demo
项目结构
项目源码
package com.supremepole.b04springbootredis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class B04SpringBootRedisApplication {
public static void main(String[] args) {
SpringApplication.run(B04SpringBootRedisApplication.class, args);
}
}
package com.supremepole.b04springbootredis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author CodeCoderCoding
*/
@RestController
public class WebsiteController {
@Autowired
RedisTemplate redisTemplate;
@Autowired
StringRedisTemplate stringRedisTemplate;
@GetMapping("/supremepole")
public void test1() {
ValueOperations<String, String> ops1 = stringRedisTemplate.opsForValue();
ops1.set("website", "supremepole");
String website1 = ops1.get("website");
System.out.println(website1);
ValueOperations ops2 = redisTemplate.opsForValue();
Website w2 = new Website();
w2.setId(1);
w2.setName("supremepole");
w2.setUrl("https://cs.supremepole.com");
ops2.set("w2", w2);
Website website2 = (Website) ops2.get("w2");
System.out.println(website2);
}
}
package com.supremepole.b04springbootredis;
import java.io.Serializable;
/**
* @author CodeCoderCoding
*/
public class Website implements Serializable {
private int id;
private String url;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return "id="+id+" ,name"+name+" ,url"+url;
}
}
server.port=8081
spring.cache.cache-names=c1,c2
spring.cache.redis.time-to-live=1800s
spring.redis.database=0
spring.redis.host=
spring.redis.port=
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