最重要的依賴java
compile('org.springframework.boot:spring-boot-starter-data-redis')
web
此依賴爲springCloud 父項目 依賴(但已添加 redis 依賴)redis
buildscript { ext { springBootVersion = '2.1.2.RELEASE' } repositories { mavenLocal() //maven本地倉庫 maven { url = "http://maven.aliyun.com/nexus/content/groups/public" } mavenCentral()//maven中心倉庫 } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } subprojects { apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.lichuang.kukri' version = '1.0.0' sourceCompatibility = 1.8 repositories { mavenLocal() //maven本地倉庫 maven { url = "http://maven.aliyun.com/nexus/content/groups/public" } mavenCentral()//maven中心倉庫 } ext { springCloudVersion = 'Greenwich.RELEASE' } dependencies { compile('org.springframework.boot:spring-boot-starter-actuator') compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.cloud:spring-cloud-starter') testCompile('org.springframework.boot:spring-boot-starter-test') //redis 依賴 compile('org.springframework.boot:spring-boot-starter-data-redis') } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } } }
由 StringRedisTemplate
的構造函數可知須要 RedisConnectionFactory
的 Bean,又由 RedisConnectionFactory
可知須要 RedisStandaloneConfiguration
的 Bean, RedisStandaloneConfiguration
的構造函數中須要有 hostname
以及 port
spring
@Configuration @ComponentScan public class AppConfig { //xxxTemplate -> 設計模式之一 模板方法設計模式 @Bean public RedisStandaloneConfiguration redisStandaloneConfiguration(){ RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("hostname",6379); return redisStandaloneConfiguration; } @Bean public RedisConnectionFactory redisConnectionFactory(){ LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration()); return connectionFactory; } @Bean public StringRedisTemplate stringRedisTemplate(){ StringRedisTemplate redisTemplate = new StringRedisTemplate(redisConnectionFactory()); return redisTemplate; } }
redisTemplate.opsForValue().set("name","test");
redisTemplate.opsForValue().get("name")
public class RedisServer { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class); redisTemplate.opsForValue().set("name","test"); //System.out.println(redisTemplate.opsForValue().get("name")); /* redisTemplate.watch("name"); redisTemplate.multi(); redisTemplate.exec();*/ } }
Spring's central cache manager SPI.shell
Cache getCache(String name);json
Collection
Interface that defines common cache operations緩存
SimpleKeyGenerator(默認實現類)app
若是緩存中有值,則使用緩存中的值;若是沒有則執行業務方法並存入緩存中less
condition
判斷
unless
非
每次都會執行業務方法,並設置緩存
每次都會執行業務方法,並刪除緩存
dependencies { compile('org.springframework.boot:spring-boot-starter-data-redis') compile group: 'org.projectlombok', name: 'lombok', version: '1.18.6' compile group: 'com.alibaba', name: 'fastjson', version: '1.2.56' }
注:
1. GenericFastJsonRedisSerializer
類 使 Value 的 儲存方式 爲 Josn
@Configuration @ComponentScan @MapperScan("com.lichuang.kukri.springcloudproject.config.dao") @EnableCaching public class AppConfig { //xxxTemplate -> 設計模式之一 模板方法設計模式 @Bean public RedisStandaloneConfiguration redisStandaloneConfiguration(){ RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("hostname",6379); return redisStandaloneConfiguration; } @Bean public CacheManager cacheManager(){ RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory()); RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericFastJsonRedisSerializer())); RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter,redisCacheConfiguration); return redisCacheManager; } }
@Service public class CacheService { @CachePut(cacheNames = "person") public Person update(int age){ Person person = new Person(); person.setPersonName("admin"); person.setAge(age); return person; } @Cacheable(cacheNames = "person") public Person selectP(int age){ Person person = new Person(); person.setPersonName("test"); person.setAge(age); return person; } @Cacheable(cacheNames = "cache") public String selectC(int i){ System.out.println("select"); return "admin"; } }
Person.java(實體類)
public class Person { private String personName; private int age; public String getPersonName() { return personName; } public void setPersonName(String personName) { this.personName = personName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
public class RedisServer { public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); PersonDao personDao = applicationContext.getBean(PersonDao.class); List<Person> people = personDao.select(); for (int i = 0; i < people.size(); i++) { System.out.println(people.get(i).getPersonName() + "-" + people.get(i).getAge()); } /*CacheService cacheService = applicationContext.getBean(CacheService.class); for (int i = 0; i < 2; i++) { //System.out.println(cacheService.selectC(i)); cacheService.update(i); }*/ } }
127.0.0.1:6379> get cache::0 "\"admin\"" 127.0.0.1:6379> get person::0 "{\"@type\":\"com.bean.Person\",\"age\":1,\"name\":\"test\"}"