摘要:添加用戶接口類簡(jiǎn)單用戶鏈接數(shù)據(jù)庫(kù)微服務(wù)通過(guò)注解標(biāo)注該類為持久化操作對(duì)象。查找用戶數(shù)據(jù)保存用戶數(shù)據(jù)更新用戶數(shù)據(jù)刪除用戶數(shù)據(jù)這是清除緩存添加緩存配置緩存配置。對(duì)象是否永久有效,一但設(shè)置了,將不起作用。設(shè)置對(duì)象在失效前允許存活時(shí)間單位秒。
SpringCloud(第 045 篇)鏈接Mysql數(shù)據(jù)庫(kù)簡(jiǎn)單的集成Mybatis、ehcache框架采用MapperXml訪問(wèn)數(shù)據(jù)庫(kù)
-
一、大致介紹1、數(shù)據(jù)庫(kù)頻繁的操作也會(huì)影響性能,所以本章節(jié)準(zhǔn)備給訪問(wèn)數(shù)據(jù)庫(kù)前面添加一層緩存操作; 2、雖然說(shuō)緩存框架存在很多且各有各的優(yōu)勢(shì),本章節(jié)僅僅只是為了測(cè)試緩存的操作實(shí)現(xiàn),所以就采用了一個(gè)簡(jiǎn)單的緩存框架ehcache;二、實(shí)現(xiàn)步驟 2.1 添加 maven 引用包
2.2 添加應(yīng)用配置文件(springms-provider-user-mysql-mybatis-mapper-ehcachesrcmainresourcesapplication.yml)4.0.0 springms-provider-user-mysql-mybatis-mapper-ehcache 1.0-SNAPSHOT jar com.springms.cloud springms-spring-cloud 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.1 org.springframework.boot spring-boot-starter-cache net.sf.ehcache ehcache
server: port: 8385 spring: application: name: springms-provider-user-mysql-mybatis-mapper-ehcache #全部小寫 ##################################################################################################### # mysql 屬性配置 datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://120.77.176.162:3306/hmilyylimh username: root password: mysqladmin # jpa: # hibernate: # #ddl-auto: create #ddl-auto:設(shè)為create表示每次都重新建表 # ddl-auto: update #ddl-auto:設(shè)為update表示每次都不會(huì)重新建表 # show-sql: true ##################################################################################################### ##################################################################################################### # mybatis mapper xml 配置 mybatis: # mybatis.type-aliases-package:指定domain類的基包,即指定其在*Mapper.xml文件中可以使用簡(jiǎn)名來(lái)代替全類名(看后邊的UserMapper.xml介紹) type-aliases-package: mapper-locations: classpath:mybatis/mapper/*.xml config-location: classpath:mybatis/mybatis-config.xml ##################################################################################################### ##################################################################################################### # 打印日志 logging: level: root: INFO org.hibernate: INFO org.hibernate.type.descriptor.sql.BasicBinder: TRACE org.hibernate.type.descriptor.sql.BasicExtractor: TRACE com.springms: DEBUG #####################################################################################################2.3 添加mybatis配置文件(springms-provider-user-mysql-mybatis-mapper-ehcache/src/main/java/com/springms/cloud/entity/User.java)
2.4 添加用戶mapperxml映射文件(springms-provider-user-mysql-mybatis-mapper-ehcache/src/main/java/com/springms/cloud/entity/User.java)
2.5 添加緩存配置文件(springms-provider-user-mysql-mybatis-mapper-ehcache/src/main/resources/ehcache.xml)update user set userName=#{username},name=#{name},age=#{age},balance=#{balance} where id=#{id}
2.6 添加實(shí)體用戶類User(sspringms-provider-user-mysql-mybatis-mapper-ehcache/src/main/java/com/springms/cloud/entity/User.java)
package com.springms.cloud.entity; public class User { private Long id; private String username; private String name; private Integer age; private String balance; /** 來(lái)自于哪里,默認(rèn)來(lái)自于數(shù)據(jù)庫(kù) */ private String from = ""; public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Integer getAge() { return this.age; } public void setAge(Integer age) { this.age = age; } public String getBalance() { return this.balance; } public void setBalance(String balance) { this.balance = balance; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } @Override public String toString() { return "User{" + "id=" + id + ", username="" + username + """ + ", name="" + name + """ + ", age=" + age + ", balance="" + balance + """ + ", from="" + from + """ + "}"; } }2.7 添加用戶mapper接口(springms-provider-user-mysql-mybatis-mapper-ehcache/src/main/java/com/springms/cloud/mapper/IUserMapper.java)
package com.springms.cloud.mapper; import com.springms.cloud.entity.User; import java.util.List; /** * 用戶 mybatis 接口文件。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017-10-19 * */ public interface IUserMapper { User findUserById(Long id); List2.8 添加用戶DAO接口類(springms-provider-user-mysql-mybatis-mapper-ehcache/src/main/java/com/springms/cloud/dao/IUserDao.java)findAllUsers(); int insertUser(User user); int updateUser(User user); int deleteUser(Long id); }
package com.springms.cloud.dao; import com.springms.cloud.entity.User; import java.util.List; /** * 簡(jiǎn)單用戶鏈接Mysql數(shù)據(jù)庫(kù)微服務(wù)(通過(guò)@Repository注解標(biāo)注該類為持久化操作對(duì)象)。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017-10-19 * */ public interface IUserDao { User findUserById(Long id); List2.9 添加用戶DAO接口類實(shí)現(xiàn)類(springms-provider-user-mysql-mybatis-mapper-ehcache/src/main/java/com/springms/cloud/dao/impl/UserDaoImpl.java)findAllUsers(); int insertUser(User user); int updateUser(User user); int deleteUser(Long id); }
package com.springms.cloud.dao.impl; import com.springms.cloud.dao.IUserDao; import com.springms.cloud.entity.User; import com.springms.cloud.mapper.IUserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import java.util.List; /** * 簡(jiǎn)單用戶鏈接Mysql數(shù)據(jù)庫(kù)微服務(wù)(通過(guò)@Repository注解標(biāo)注該類為持久化操作對(duì)象)。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017-10-19 * */ @Repository public class UserDaoImpl implements IUserDao { @Autowired private IUserMapper iUserMapper; @Override public User findUserById(Long id) { return iUserMapper.findUserById(id); } @Override public List2.10 添加用戶Service接口類(springms-provider-user-mysql-mybatis-mapper-ehcache/src/main/java/com/springms/cloud/service/IUserService.java)findAllUsers() { return iUserMapper.findAllUsers(); } @Override public int insertUser(User user) { return iUserMapper.insertUser(user); } @Override public int updateUser(User user) { return iUserMapper.updateUser(user); } @Override public int deleteUser(Long id) { return iUserMapper.deleteUser(id); } }
package com.springms.cloud.service; import com.springms.cloud.entity.User; import java.util.List; /** * 簡(jiǎn)單用戶鏈接Mysql數(shù)據(jù)庫(kù)微服務(wù)(通過(guò)@Service注解標(biāo)注該類為持久化操作對(duì)象)。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017-10-19 * */ public interface IUserService { User findUserById(Long id); List2.11 添加用戶Service接口實(shí)現(xiàn)類(springms-provider-user-mysql-mybatis-mapper-ehcache/src/main/java/com/springms/cloud/service/impl/UserServiceImpl.java)findAllUsers(); int insertUser(User user); int updateUser(User user); int deleteUser(Long id); }
package com.springms.cloud.service.impl; import com.springms.cloud.dao.IUserDao; import com.springms.cloud.entity.User; import com.springms.cloud.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.List; /** * 簡(jiǎn)單用戶鏈接Mysql數(shù)據(jù)庫(kù)微服務(wù)(通過(guò)@Service注解標(biāo)注該類為持久化操作對(duì)象)。
* *
package com.springms.cloud.config; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.cache.ehcache.EhCacheManagerFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; /** * 緩存配置。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017-10-19 * */ @Configuration @EnableCaching//標(biāo)注啟動(dòng)緩存. public class CacheConfiguration { /** * ehcache 主要的管理器 * * @param bean * @return */ @Bean public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){ System.out.println("CacheConfiguration.ehCacheCacheManager()"); return new EhCacheCacheManager(bean.getObject()); } /* * 據(jù)shared與否的設(shè)置, * Spring分別通過(guò)CacheManager.create() * 或new CacheManager()方式來(lái)創(chuàng)建一個(gè)ehcache基地. * * 也說(shuō)是說(shuō)通過(guò)這個(gè)來(lái)設(shè)置cache的基地是這里的Spring獨(dú)用,還是跟別的(如hibernate的Ehcache共享) * */ @Bean public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){ System.out.println("CacheConfiguration.ehCacheManagerFactoryBean()"); EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean (); cacheManagerFactoryBean.setConfigLocation (new ClassPathResource("ehcache.xml")); cacheManagerFactoryBean.setShared(true); return cacheManagerFactoryBean; } }2.13 添加用戶Web訪問(wèn)層Controller(springms-provider-user-mysql-mybatis-mapper/src/main/java/com/springms/cloud/controller/ProviderUserMysqlMybatisMapperController.java)
package com.springms.cloud.controller; import com.springms.cloud.entity.User; import com.springms.cloud.service.IUserService; import org.hibernate.cache.CacheException; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; /** * 用戶微服務(wù)Controller。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017-10-19 * */ @RestController public class ProviderUserMysqlMybatisMapperEhCacheController { private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(ProviderUserMysqlMybatisMapperEhCacheController.class); @Autowired private IUserService iUserService; @GetMapping("/user/{id}") public User findUserById(@PathVariable Long id) { return this.iUserService.findUserById(id); } @GetMapping("/user/list") public List2.14 添加微服務(wù)啟動(dòng)類(springms-provider-user-mysql-mybatis-mapper-ehcache/src/main/java/com/springms/cloud/MsProviderUserMysqlMybatisMapperEhCacheApplication.java)findUserList() { return this.iUserService.findAllUsers(); } /** * 添加一個(gè)student,使用postMapping接收post請(qǐng)求 * * http://localhost:8330/simple/addUser?username=user11&age=11&balance=11 * * @return */ @PostMapping("/user/addUser") public User addUser(@RequestParam(value = "username", required=false) String username, @RequestParam(value = "age", required=false) Integer age, @RequestParam(value = "balance", required=false) String balance){ User user=new User(); user.setUsername(username); user.setName(username); user.setAge(age); user.setBalance(balance); int result = iUserService.insertUser(user); if(result > 0){ return user; } user.setId(0L); user.setName(null); user.setUsername(null); user.setBalance(null); return user; } @GetMapping("/user/ehcache") public String ehcache() { Logger.info("=========== 進(jìn)行Encache緩存測(cè)試"); List allUsers = iUserService.findAllUsers(); User lastUser = allUsers.get(allUsers.size() - 1); String lastUserUsername = lastUser.getUsername(); String indexString = lastUserUsername.substring(4); Logger.info("=========== ====生成第一個(gè)用戶===="); User user1 = new User(); //生成第一個(gè)用戶的唯一標(biāo)識(shí)符 UUID user1.setName("user" + (Integer.parseInt(indexString) + 1)); user1.setUsername(user1.getName()); user1.setAge(1000); user1.setBalance("1000"); if (iUserService.insertUser(user1) == 0){ throw new CacheException("用戶對(duì)象插入數(shù)據(jù)庫(kù)失敗"); } allUsers = iUserService.findAllUsers(); lastUser = allUsers.get(allUsers.size() - 1); Long lastUserId = lastUser.getId(); //第一次查詢 Logger.info("=========== 第一次查詢"); Logger.info("=========== 第一次查詢結(jié)果: {}", iUserService.findUserById(lastUserId)); //通過(guò)緩存查詢 Logger.info("=========== 通過(guò)緩存第 1 次查詢"); Logger.info("=========== 通過(guò)緩存第 1 次查詢結(jié)果: {}", iUserService.findUserById(lastUserId)); Logger.info("=========== 通過(guò)緩存第 2 次查詢"); Logger.info("=========== 通過(guò)緩存第 2 次查詢結(jié)果: {}", iUserService.findUserById(lastUserId)); Logger.info("=========== 通過(guò)緩存第 3 次查詢"); Logger.info("=========== 通過(guò)緩存第 3 次查詢結(jié)果: {}", iUserService.findUserById(lastUserId)); Logger.info("=========== ====準(zhǔn)備修改數(shù)據(jù)===="); User user2 = new User(); user2.setName(lastUser.getName()); user2.setUsername(lastUser.getUsername()); user2.setAge(lastUser.getAge() + 1000); user2.setBalance(String.valueOf(user2.getAge())); user2.setId(lastUserId); try { int result = iUserService.updateUser(user2); Logger.info("=========== ==== 修改數(shù)據(jù) == {} ==", (result > 0? "成功":"失敗")); } catch (CacheException e){ e.printStackTrace(); } Logger.info("=========== ====修改后再次查詢數(shù)據(jù)"); Object resultObj = iUserService.findUserById(lastUser.getId()); Logger.info("=========== ====修改后再次查詢數(shù)據(jù)結(jié)果: {}", resultObj); return "success"; } }
package com.springms.cloud; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; /** * 鏈接Mysql數(shù)據(jù)庫(kù)簡(jiǎn)單的集成Mybatis、ehcache框架采用MapperXml訪問(wèn)數(shù)據(jù)庫(kù)。 * * 簡(jiǎn)單用戶鏈接Mysql數(shù)據(jù)庫(kù)微服務(wù)(通過(guò) mybatis 鏈接 mysql 并用 MapperXml 編寫數(shù)據(jù)訪問(wèn),并且通過(guò) EhCache 緩存來(lái)訪問(wèn))。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017-10-19 * */ @SpringBootApplication @MapperScan("com.springms.cloud.mapper.**") @EnableCaching public class MsProviderUserMysqlMybatisMapperEhCacheApplication { public static void main(String[] args) { SpringApplication.run(MsProviderUserMysqlMybatisMapperEhCacheApplication.class, args); System.out.println("【【【【【【 鏈接MysqlMybatisMapperEhCache數(shù)據(jù)庫(kù)微服務(wù) 】】】】】】已啟動(dòng)."); } }三、測(cè)試
/**************************************************************************************** 注意:Mybatis 需要加上 entity 等注解才可以使用,不然啟動(dòng)都會(huì)報(bào)錯(cuò); @MapperScan("com.springms.cloud.mapper.**") 或者在每個(gè) Mapper 接口文件上添加 @Mapper 也可以; 一、簡(jiǎn)單用戶鏈接Mysql數(shù)據(jù)庫(kù)微服務(wù)(通過(guò) mybatis 鏈接 mysql 并用 MapperXml 編寫數(shù)據(jù)訪問(wèn),并且通過(guò) EhCache 緩存來(lái)訪問(wèn)): 1、啟動(dòng) springms-provider-user-mysql-mybatis-mapper-ehcache 模塊服務(wù),啟動(dòng)1個(gè)端口; 2、在瀏覽器輸入地址 http://localhost:8385/user/10 可以看到用戶ID=10的信息成功的被打印出來(lái); 3、使用 IDEA 自帶工具 Test Restful WebService 發(fā)送 HTTP POST 請(qǐng)求,并添加 username、age、balance三個(gè)參數(shù),然后執(zhí)行請(qǐng)求,并去 mysql 數(shù)據(jù)庫(kù)查看數(shù)據(jù)是否存在,正常情況下 mysql 數(shù)據(jù)庫(kù)剛剛插入的數(shù)據(jù)成功了: 4、使用 REST Client 執(zhí)行 "/user/ehcache" 接口,也正常將 mysql 數(shù)據(jù)庫(kù)中所有的用戶信息打印出來(lái)了,并且打印的信息如下: 5、在瀏覽器輸入地址 http://localhost:8385/user/ehcache 可以看到如下信息被打印出來(lái); 2017-10-19 17:48:54.561 INFO 2736 --- [nio-8385-exec-1] erMysqlMybatisMapperXmlEhCacheController : =========== 進(jìn)行Encache緩存測(cè)試 2017-10-19 17:48:54.610 DEBUG 2736 --- [nio-8385-exec-1] c.i.c.mapper.IUserMapper.findAllUsers : ==> Preparing: select * from user 2017-10-19 17:48:54.629 DEBUG 2736 --- [nio-8385-exec-1] c.i.c.mapper.IUserMapper.findAllUsers : ==> Parameters: 2017-10-19 17:48:54.657 DEBUG 2736 --- [nio-8385-exec-1] c.i.c.mapper.IUserMapper.findAllUsers : <== Total: 65 2017-10-19 17:48:54.658 INFO 2736 --- [nio-8385-exec-1] erMysqlMybatisMapperXmlEhCacheController : =========== ====生成第一個(gè)用戶==== 2017-10-19 17:48:54.661 DEBUG 2736 --- [nio-8385-exec-1] c.i.cloud.mapper.IUserMapper.insertUser : ==> Preparing: INSERT INTO user ( username, name, age, balance ) VALUES ( ?, ?, ?, ? ) 2017-10-19 17:48:54.662 DEBUG 2736 --- [nio-8385-exec-1] c.i.cloud.mapper.IUserMapper.insertUser : ==> Parameters: user66(String), user66(String), 1000(Integer), 1000(String) 2017-10-19 17:48:54.678 DEBUG 2736 --- [nio-8385-exec-1] c.i.cloud.mapper.IUserMapper.insertUser : <== Updates: 1 2017-10-19 17:48:54.691 DEBUG 2736 --- [nio-8385-exec-1] c.i.c.mapper.IUserMapper.findAllUsers : ==> Preparing: select * from user 2017-10-19 17:48:54.692 DEBUG 2736 --- [nio-8385-exec-1] c.i.c.mapper.IUserMapper.findAllUsers : ==> Parameters: 2017-10-19 17:48:54.707 DEBUG 2736 --- [nio-8385-exec-1] c.i.c.mapper.IUserMapper.findAllUsers : <== Total: 66 2017-10-19 17:48:54.708 INFO 2736 --- [nio-8385-exec-1] erMysqlMybatisMapperXmlEhCacheController : =========== 第一次查詢 2017-10-19 17:48:54.714 DEBUG 2736 --- [nio-8385-exec-1] c.i.c.mapper.IUserMapper.findUserById : ==> Preparing: select * from user where id = ? 2017-10-19 17:48:54.714 DEBUG 2736 --- [nio-8385-exec-1] c.i.c.mapper.IUserMapper.findUserById : ==> Parameters: 147(Long) 2017-10-19 17:48:54.721 DEBUG 2736 --- [nio-8385-exec-1] c.i.c.mapper.IUserMapper.findUserById : <== Total: 1 2017-10-19 17:48:54.722 INFO 2736 --- [nio-8385-exec-1] erMysqlMybatisMapperXmlEhCacheController : =========== 第一次查詢結(jié)果: User{id=147, username="user66", name="user66", age=1000, balance="1000", from=""} 2017-10-19 17:48:54.722 INFO 2736 --- [nio-8385-exec-1] erMysqlMybatisMapperXmlEhCacheController : =========== 通過(guò)緩存第 1 次查詢 2017-10-19 17:48:54.723 INFO 2736 --- [nio-8385-exec-1] erMysqlMybatisMapperXmlEhCacheController : =========== 通過(guò)緩存第 1 次查詢結(jié)果: User{id=147, username="user66", name="user66", age=1000, balance="1000", from=""} 2017-10-19 17:48:54.723 INFO 2736 --- [nio-8385-exec-1] erMysqlMybatisMapperXmlEhCacheController : =========== 通過(guò)緩存第 2 次查詢 2017-10-19 17:48:54.724 INFO 2736 --- [nio-8385-exec-1] erMysqlMybatisMapperXmlEhCacheController : =========== 通過(guò)緩存第 2 次查詢結(jié)果: User{id=147, username="user66", name="user66", age=1000, balance="1000", from=""} 2017-10-19 17:48:54.724 INFO 2736 --- [nio-8385-exec-1] erMysqlMybatisMapperXmlEhCacheController : =========== 通過(guò)緩存第 3 次查詢 2017-10-19 17:48:54.724 INFO 2736 --- [nio-8385-exec-1] erMysqlMybatisMapperXmlEhCacheController : =========== 通過(guò)緩存第 3 次查詢結(jié)果: User{id=147, username="user66", name="user66", age=1000, balance="1000", from=""} 2017-10-19 17:48:54.724 INFO 2736 --- [nio-8385-exec-1] erMysqlMybatisMapperXmlEhCacheController : =========== ====準(zhǔn)備修改數(shù)據(jù)==== 2017-10-19 17:48:54.725 DEBUG 2736 --- [nio-8385-exec-1] c.i.cloud.mapper.IUserMapper.updateUser : ==> Preparing: update user set userName=?,name=?,age=?,balance=? where id=? 2017-10-19 17:48:54.725 DEBUG 2736 --- [nio-8385-exec-1] c.i.cloud.mapper.IUserMapper.updateUser : ==> Parameters: user66(String), user66(String), 2000(Integer), 2000(String), 147(Long) 2017-10-19 17:48:54.738 DEBUG 2736 --- [nio-8385-exec-1] c.i.cloud.mapper.IUserMapper.updateUser : <== Updates: 1 2017-10-19 17:48:54.747 INFO 2736 --- [nio-8385-exec-1] erMysqlMybatisMapperXmlEhCacheController : =========== ==== 修改數(shù)據(jù) == 成功 == 2017-10-19 17:48:54.747 INFO 2736 --- [nio-8385-exec-1] erMysqlMybatisMapperXmlEhCacheController : =========== ====修改后再次查詢數(shù)據(jù) 2017-10-19 17:48:54.747 DEBUG 2736 --- [nio-8385-exec-1] c.i.c.mapper.IUserMapper.findUserById : ==> Preparing: select * from user where id = ? 2017-10-19 17:48:54.747 DEBUG 2736 --- [nio-8385-exec-1] c.i.c.mapper.IUserMapper.findUserById : ==> Parameters: 147(Long) 2017-10-19 17:48:54.747 DEBUG 2736 --- [nio-8385-exec-1] c.i.c.mapper.IUserMapper.findUserById : <== Total: 1 2017-10-19 17:48:54.747 INFO 2736 --- [nio-8385-exec-1] erMysqlMybatisMapperXmlEhCacheController : =========== ====修改后再次查詢數(shù)據(jù)結(jié)果: User{id=147, username="user66", name="user66", age=2000, balance="2000", from=""} 總結(jié):可以看出查詢過(guò)一次后,就不會(huì)再查詢數(shù)據(jù)庫(kù)了,所以第二次、第三次都會(huì)去查找緩存的數(shù)據(jù); ****************************************************************************************/ /**************************************************************************************** Ehcache 相關(guān)資料: diskStore:為緩存路徑,ehcache分為內(nèi)存和磁盤兩級(jí),此屬性定義磁盤的緩存位置。 defaultCache:默認(rèn)緩存策略,當(dāng)ehcache找不到定義的緩存時(shí),則使用這個(gè)緩存策略。只能定義一個(gè)。 name:緩存名稱。 maxElementsInMemory:緩存最大數(shù)目 maxElementsOnDisk:硬盤最大緩存?zhèn)€數(shù)。 eternal:對(duì)象是否永久有效,一但設(shè)置了,timeout將不起作用。 overflowToDisk:是否保存到磁盤,當(dāng)系統(tǒng)當(dāng)機(jī)時(shí) timeToIdleSeconds:設(shè)置對(duì)象在失效前的允許閑置時(shí)間(單位:秒)。僅當(dāng)eternal=false對(duì)象不是永久有效時(shí)使用,可選屬性,默認(rèn)值是0,也就是可閑置時(shí)間無(wú)窮大。 timeToLiveSeconds:設(shè)置對(duì)象在失效前允許存活時(shí)間(單位:秒)。最大時(shí)間介于創(chuàng)建時(shí)間和失效時(shí)間之間。僅當(dāng)eternal=false對(duì)象不是永久有效時(shí)使用,默認(rèn)是0.,也就是對(duì)象存活時(shí)間無(wú)窮大。 diskPersistent:是否緩存虛擬機(jī)重啟期數(shù)據(jù) Whether the disk store persists between restarts of the Virtual Machine. The default value is false.diskSpoolBufferSizeMB:這個(gè)參數(shù)設(shè)置DiskStore(磁盤緩存)的緩存區(qū)大小。默認(rèn)是30MB。每個(gè)Cache都應(yīng)該有自己的一個(gè)緩沖區(qū)。 diskExpiryThreadIntervalSeconds:磁盤失效線程運(yùn)行時(shí)間間隔,默認(rèn)是120秒。 memoryStoreEvictionPolicy:當(dāng)達(dá)到maxElementsInMemory限制時(shí),Ehcache將會(huì)根據(jù)指定的策略去清理內(nèi)存。默認(rèn)策略是LRU(最近最少使用)。你可以設(shè)置為FIFO(先進(jìn)先出)或是LFU(較少使用)。 clearOnFlush:內(nèi)存數(shù)量最大時(shí)是否清除。 memoryStoreEvictionPolicy:可選策略有:LRU(最近最少使用,默認(rèn)策略)、FIFO(先進(jìn)先出)、LFU(最少訪問(wèn)次數(shù))。 FIFO,first in first out,先進(jìn)先出。 LFU, Less Frequently Used,一直以來(lái)最少被使用的。如上面所講,緩存的元素有一個(gè)hit屬性,hit值最小的將會(huì)被清出緩存。 LRU,Least Recently Used,最近最少使用的,緩存的元素有一個(gè)時(shí)間戳,當(dāng)緩存容量滿了,而又需要騰出地方來(lái)緩存新的元素的時(shí)候,那么現(xiàn)有緩存元素中時(shí)間戳離當(dāng)前時(shí)間最遠(yuǎn)的元素將被清出緩存。 一般情況下,我們?cè)赟ercive層進(jìn)行對(duì)緩存的操作。先介紹 Ehcache 在 Spring 中的注解:在支持 Spring Cache 的環(huán)境下, * @Cacheable : Spring在每次執(zhí)行前都會(huì)檢查Cache中是否存在相同key的緩存元素,如果存在就不再執(zhí)行該方法,而是直接從緩存中獲取結(jié)果進(jìn)行返回,否則才會(huì)執(zhí)行并將返回結(jié)果存入指定的緩存中。 * @CacheEvict : 清除緩存。 * @CachePut : @CachePut也可以聲明一個(gè)方法支持緩存功能。使用@CachePut標(biāo)注的方法在執(zhí)行前不會(huì)去檢查緩存中是否存在之前執(zhí)行過(guò)的結(jié)果,而是每次都會(huì)執(zhí)行該方法,并將執(zhí)行結(jié)果以鍵值對(duì)的形式存入指定的緩存中。 * 這三個(gè)方法中都有兩個(gè)主要的屬性:value 指的是 ehcache.xml 中的緩存策略空間;key 指的是緩存的標(biāo)識(shí),同時(shí)可以用 # 來(lái)引用參數(shù)。 ****************************************************************************************/四、下載地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
SpringCloudTutorial交流QQ群: 235322432
SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接
歡迎關(guān)注,您的肯定是對(duì)我最大的支持!!!
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/67839.html
摘要:添加用戶接口類實(shí)現(xiàn)類簡(jiǎn)單用戶鏈接數(shù)據(jù)庫(kù)微服務(wù)通過(guò)注解標(biāo)注該類為持久化操作對(duì)象。添加一個(gè)使用接收請(qǐng)求添加微服務(wù)啟動(dòng)類鏈接數(shù)據(jù)庫(kù)簡(jiǎn)單的集成框架采用訪問(wèn)數(shù)據(jù)庫(kù)。 SpringCloud(第 044 篇)鏈接Mysql數(shù)據(jù)庫(kù)簡(jiǎn)單的集成Mybatis框架采用MapperXml訪問(wèn)數(shù)據(jù)庫(kù) - 一、大致介紹 1、前面章節(jié)講解的是在方法上面添加sql語(yǔ)句操作,雖然說(shuō)僅僅只是一種簡(jiǎn)單的操作,在測(cè)試期間可...
摘要:添加用戶接口實(shí)現(xiàn)類簡(jiǎn)單用戶鏈接數(shù)據(jù)庫(kù)微服務(wù)通過(guò)注解標(biāo)注該類為持久化操作對(duì)象。添加一個(gè)使用接收請(qǐng)求添加微服務(wù)啟動(dòng)類鏈接數(shù)據(jù)庫(kù)簡(jiǎn)單的集成框架訪問(wèn)數(shù)據(jù)庫(kù)。 SpringCloud(第 043 篇)鏈接Mysql數(shù)據(jù)庫(kù)簡(jiǎn)單的集成Mybatis框架訪問(wèn)數(shù)據(jù)庫(kù) - 一、大致介紹 1、訪問(wèn)數(shù)據(jù)庫(kù),自然少不了一些持久化框架,而我本身也是Mybatis框架的支持者; 2、Mybatis是那種專注于sql...
閱讀 2857·2021-09-10 10:51
閱讀 2222·2021-09-02 15:21
閱讀 3214·2019-08-30 15:44
閱讀 882·2019-08-29 18:34
閱讀 1662·2019-08-29 13:15
閱讀 3331·2019-08-26 11:37
閱讀 2706·2019-08-26 10:46
閱讀 1118·2019-08-26 10:26