三、Memcached整合SpringBoot
说明:SpringBoot并没有收录memcached为缓存解决方案,因此使用memcached需要通过手工硬编码的方式来使用。
memcached目前提供有三种客户端技术
Memcached Client for Java、
SpyMemcached、
Xmemcached,
其中性能指标各方面最好的客户端是Xmemcached,本次整合就使用Xmemcached。
(1)导入xmemcached的坐标
<dependency> <groupId>com.googlecode.xmemcached</groupId> <artifactId>xmemcached</artifactId> <version>2.4.7</version> </dependency>
(2)配置memcached,制作memcached的配置类
说明:memcached默认对外服务端口11211。
package com.test; import net.rubyeye.xmemcached.MemcachedClient; import net.rubyeye.xmemcached.MemcachedClientBuilder; import net.rubyeye.xmemcached.XMemcachedClientBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.io.IOException; @Configuration public class XMemcachedConfig { @Bean public MemcachedClient getMemcachedClient() throws IOException { MemcachedClientBuilder memcachedClientBuilder = new XMemcachedClientBuilder("localhost:11211"); MemcachedClient memcachedClient = memcachedClientBuilder.build(); return memcachedClient; } }
(3)使用xmemcached客户端操作缓存,注入MemcachedClient对象
code实体类
package com.test; import lombok.Data; @Data public class SMSCode { private String tele; private String code; }
code工具类
@Component public class CodeUtils { private String [] patch = {"000000","00000","0000","000","00","0",""}; public String generator(String tele){ int hash = tele.hashCode(); int encryption = 20206666; long result = hash ^ encryption; long nowTime = System.currentTimeMillis(); result = result ^ nowTime; long code = result % 1000000; code = code < 0 ? -code : code; String codeStr = code + ""; int len = codeStr.length(); return patch[len] + codeStr; } @Cacheable(value = "smsCode",key="#tele") public String get(String tele){ return null; } }
code接口
package com.test; public interface SMSCodeService { }
code实现类
@Service public class SMSCodeServiceImpl implements SMSCodeService { @Autowired private CodeUtils codeUtils; @Autowired private MemcachedClient memcachedClient; public String sendCodeToSMS(String tele) { String code = codeUtils.generator(tele); try { memcachedClient.set(tele,10,code); } catch (Exception e) { e.printStackTrace(); } return code; } public boolean checkCode(SMSCode smsCode) { String code = null; try { code = memcachedClient.get(smsCode.getTele()).toString(); } catch (Exception e) { e.printStackTrace(); } return smsCode.getCode().equals(code); } }
说明:设置值到缓存中使用set操作,取值使用get操作,其实更符合我们开发者的习惯。
(4)定义配置属性
a.定义配置类,加载必要的配置属性
读取配置文件中memcached节点信息
@Component @ConfigurationProperties(prefix = "memcached") @Data public class XMemcachedProperties { private String servers; private int poolSize; private long opTimeout; }
b.定义memcached节点信息
memcached: servers: localhost:11211 poolSize: 10 opTimeout: 3000
c.在memcached配置类中加载信息
@Configuration public class XMemcachedConfig { @Autowired private XMemcachedProperties props; @Bean public MemcachedClient getMemcachedClient() throws IOException { MemcachedClientBuilder memcachedClientBuilder = new XMemcachedClientBuilder(props.getServers()); memcachedClientBuilder.setConnectionPoolSize(props.getPoolSize()); memcachedClientBuilder.setOpTimeout(props.getOpTimeout()); MemcachedClient memcachedClient = memcachedClientBuilder.build(); return memcachedClient; } }