
这个可以看做一个框架,对其他的缓存进行了整合。
- <dependency>
- <groupId>com.alicp.jetcache</groupId>
- <artifactId>jetcache-starter-redis</artifactId>
- <version>2.6.2</version>
- </dependency
- # jetcache配置
- jetcache:
- # 远程连接方案
- remote:
- # 只有一种情况的时候,就用default
- default:
- # 使用redis
- type: redis
- host: localhost
- port: 6379
- # 这个地方最少要给一条,不然会报错
- poolConfig:
- maxTotal: 50
- # 定义另外一组配置
- sms:
- # 使用redis
- type: redis
- host: localhost
- port: 6379
- # 这个地方最少要给一条,不然会报错
- poolConfig:
- maxTotal: 50
在启动类添加@EnableCreateCacheAnnotation 注解
- @SpringBootApplication
- @EnableCreateCacheAnnotation //启用使用注解的方式创建缓存
- public class Springboot19CacheApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(Springboot19CacheApplication.class, args);
- }
-
- }
启动服务器

启动客户端

类似集合的操作
- @Service
- public class SMSCodeServiceImpl implements SMSCodeService {
-
-
- @Autowired
- private CodeUtils codeUtils;
-
- // area指定配置(如果不指定默认default配置) name 缓存空间 expire 缓存过期时间,默认是秒 timeUnit是指定expire的单位
- @CreateCache(area = "sms",name = "jetCache_",expire = 3600,timeUnit = TimeUnit.SECONDS)
- // 用com.alicp.jetcache.Cache
- // 泛型根据实际情况进行指定
- private Cache<String,String> jetCache;
-
- @Override
-
- public String sendCodeToSMS(String tele) {
- // 通过电话号码得到我们想要的验证码
- String code= codeUtils.generator(tele);
-
- // 放入缓存
- jetCache.put(tele,code);
-
- return code;
- }
-
- @Override
- public boolean checkCode(SMSCode smsCode) {
- // 取出内存中的验证码与传递过来的验证码进行对比,如果相同,返回true
- //用户输入的验证码以及电话号码
- String code = smsCode.getCode();
-
- //这是缓存中的验证码,因为tele是key,所以在此处要传入一个tele(正确的验证码)
- String cacheCode =jetCache.get(smsCode.getTele());
-
- return code.equals(cacheCode);
- }
- }

