说到redis缓存,我想先说下spring的默认缓存。spring对缓存的支持,是有着一套接入第三方缓存的规范的。第三方的缓存,如redis、Memcached等,若是须要在spring中使用,就要实现CacheManage。 使用spring缓存,通常须要用到三个注解:@Cacheable,@CachePut,@CacheEvict。html
@Cacheable,可用于类上或者方法上。表示方法的返回值能够被缓存。当标记在一个类上时则表示该类全部的方法都是支持缓存的。对于一个支持缓存的方法,Spring会在其被调用后将其返回值缓存起来,以保证下次利用一样的参数来执行该方法时能够直接从缓存中获取结果,而不须要再次执行该方法。spring的缓存值是以键值对形式存在的,默认使用方法参数做为key值,能够自定义key值生成策略,或自定义key值。@Cacheable能够指定两个属性,value、key。 value属性是必须指定的,其表示当前方法的返回值是会被缓存在哪一个Cache上的,对应Cache的名称。 key属性是用来指定Spring缓存方法的返回结果时对应的key的。该属性支持SpringEL表达式。当咱们没有指定该属性时,Spring将使用默认策略生成key。java
@Cacheable(value = "coms", key = "#id") public Component get(String id) { return crudService.get(id, Component.class); }
上例中,就是指定了get方法的缓存名称为coms,key值为传入的id值,缓存值为Component。web
@CachePut,能够用于类上或者方法上。表示方法的返回值能够被更新和缓存。@CachePut标注的方法在执行前不会去检查缓存中是否存在以前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。@Cacheable和@CachePut常常联合使用,可是前提是指定相同的缓存名称和缓存key值。以下:redis
@Cacheable(value = "coms", key = "#id") public Component get(String id) { return crudService.get(id, Component.class); } @CachePut(value = "coms", key = "#id") public Component update(String id) { Component component = crudService.get(id, Component.class); component.setCreateTime(new Date()); crudService.update(component); return component; }
调用update方法时,也会同步更新get方法对应的缓存。spring
@CacheEvict,是用来标注在须要清除缓存元素的方法或类上的。当标记在一个类上时表示其中全部的方法的执行都会触发缓存的清除操做。@CacheEvict能够指定的重要属性包括allEntries,allEntries=true则表示整个缓存名所表明的的缓存都被清空。数据库
@CacheEvict(value = "coms", allEntries = true) public void delete(String id) { crudService.delete(id, Component.class); }
1. 新建springboot项目,加入maven依赖json
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.0.RELEASE</version> </dependency>
2. 新建启动类,加入@EnableCaching浏览器
@SpringBootApplication @EnableCaching public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } }
3. 至此 ,项目已经能够启动,使用默认端口8080。一个很是简单的springboot项目搭好了。接来下编写缓存测试类。因为spring-context包已经提供了默认的缓存类,因此不须要额外都缓存依赖。缓存
@RequestMapping @RestController public class TestCacheController { @GetMapping("/get") @Cacheable(value = "user", key = "#id") public User get(String id) { System.out.println("调用方法自己--get"); User user = new User(); user.setId(id); user.setName("第一"); return user; } @GetMapping("/update") @CachePut(value = "user", key = "#id") public User update(String id,String name) { System.out.println("调用方法自己--update"); User user = new User(); user.setId(id); user.setName(name); return user; } @GetMapping("/delete") @CacheEvict(value = "user", key = "#id") public void delete(String id) { System.out.println("调用方法自己--delete"); } }
public class User implements Serializable{ private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
4.使用浏览器,输入请求访问屡次测试结果,对比控制台的输出,验证每一个注解是否正常发挥做用。我这边彻底已经正常了,就不贴出截图了。springboot
一直都以为redis是多么高大上的东西,毕竟历来没用过。
Redis是一个高性能的key-value型数据库,同时还提供list,set,zset,hash等数据结构的存储,支持数据的持久化。Redis的全部操做都是原子性的。具体的安装、命令教程均可以从官网查看:http://www.redis.net.cn/tutorial/3501.html。
总结来讲,就是一个redis分服务端和客户端两部分,服务端负责存储数据,客户端链接服务端以后,以命令的形式控制服务端。redis一般被用做系统和数据库之间的临时内存,系统从redis中获取数据比从数据库中直接获取数据会快不少。
redis集成到spring中,须要实现CacheManager。并导入相关的依赖和配置。
1. 使用上文搭建好的springboot做为基础,集成redis。
2.加入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.1.0.RELEASE</version> </dependency>
3. 加入application.properties的相关配置
# Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redis.host=192.168.65.45 # Redis服务器链接端口 spring.redis.port=6379 # Redis服务器链接密码(默认为空) spring.redis.password=test # 链接池最大链接数(使用负值表示没有限制) spring.redis.jedis.pool.max-active=8 # 链接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=-1 # 链接池中的最大空闲链接 spring.redis.jedis.pool.max-idle=8 # 链接池中的最小空闲链接 spring.redis.jedis.pool.min-idle=2 # 链接超时时间(毫秒) spring.redis.timeout=3000
4. 加入配置类
@Configuration public class RedisConfig extends CachingConfigurerSupport { @Bean("userTemplate") public RedisTemplate<String, User> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, User> userRedisTemplate = new RedisTemplate<String, User>(); Jackson2JsonRedisSerializer<User> userJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<User>(User.class); userRedisTemplate.setValueSerializer(userJackson2JsonRedisSerializer); userRedisTemplate.setHashValueSerializer(userJackson2JsonRedisSerializer); userRedisTemplate.setKeySerializer(new StringRedisSerializer()); userRedisTemplate.setHashKeySerializer(new StringRedisSerializer()); userRedisTemplate.setConnectionFactory(redisConnectionFactory); return userRedisTemplate; } }
5.编写测试类
@RequestMapping("/redis") @RestController public class TestRedisController { @Autowired @Qualifier("userTemplate") private RedisTemplate<String,User> redisTemplate; @GetMapping("/get") public User get(String id) { if (redisTemplate.opsForValue().get("user")!=null){ return redisTemplate.opsForValue().get("user"); } System.out.println("调用方法自己--get"); User user = new User(); user.setId(id); user.setName("第一"); redisTemplate.opsForValue().set("user",user); return user; } }
浏览器访问,第一次输出“调用方法自己--get”,可是第二次没有输出在,证实缓存可用。而且,经过redis-cli链接到redis服务器,输入“get user” ,就会发现确实存在着这么一个缓存: