使用Redis做集中式缓存
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.didispace</groupId>
|
||||
<artifactId>Chapter4-4-2</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter4-4-2</name>
|
||||
<description>Spring Boot project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.21</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-cache</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/3/23 下午2:34.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableCaching
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.didispace.domain;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/3/21 下午3:35.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@Entity
|
||||
public class User implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@Column(nullable = false)
|
||||
private Integer age;
|
||||
|
||||
public User(){}
|
||||
|
||||
public User(String name, Integer age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.didispace.domain;
|
||||
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/3/23 下午2:34.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@CacheConfig(cacheNames = "users")
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
|
||||
@Cacheable(key = "#p0")
|
||||
User findByName(String name);
|
||||
|
||||
@CachePut(key = "#p0.name")
|
||||
User save(User user);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/test
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=123456
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
|
||||
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
|
||||
spring.jpa.properties.hibernate.show_sql=true
|
||||
|
||||
spring.redis.host=localhost
|
||||
spring.redis.port=6379
|
||||
spring.redis.pool.max-idle=8
|
||||
spring.redis.pool.min-idle=0
|
||||
spring.redis.pool.max-active=8
|
||||
spring.redis.pool.max-wait=-1
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.didispace;
|
||||
|
||||
import com.didispace.domain.User;
|
||||
import com.didispace.domain.UserRepository;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/3/23 下午2:34.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(Application.class)
|
||||
public class ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private CacheManager cacheManager;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
userRepository.save(new User("AAA", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
|
||||
User u1 = userRepository.findByName("AAA");
|
||||
System.out.println("第一次查询:" + u1.getAge());
|
||||
|
||||
User u2 = userRepository.findByName("AAA");
|
||||
System.out.println("第二次查询:" + u2.getAge());
|
||||
|
||||
u1.setAge(20);
|
||||
userRepository.save(u1);
|
||||
User u3 = userRepository.findByName("AAA");
|
||||
System.out.println("第三次查询:" + u3.getAge());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user