麻涌公司网站建设公司,学好seo,如何做网站后台,wordpress中footer函数我的上一个博客演示了Spring 3.1的Cacheable批注的应用#xff0c; Cacheable批注用于标记返回值将存储在缓存中的方法。 但是#xff0c; Cacheable只是Spring的Guy为缓存而设计的一对注释中的一个#xff0c;另一个是CacheEvict 。 像Cacheable一样#xff0c; Cache… 我的上一个博客演示了Spring 3.1的Cacheable批注的应用 Cacheable批注用于标记返回值将存储在缓存中的方法。 但是 Cacheable只是Spring的Guy为缓存而设计的一对注释中的一个另一个是CacheEvict 。 像Cacheable一样 CacheEvict具有value key和condition属性。 它们的工作方式与Cacheable支持的方式Cacheable 因此有关它们的更多信息请参见我以前的博客 Spring 3.1 Caching和Cacheable 。 CacheEvict支持两个附加属性 allEntries和beforeInvocation 。 如果我是一个赌博的人我会花钱买最受欢迎的allEntries 。 allEntries用于完全清除CacheEvict的强制value参数定义的高速缓存的内容。 下面的方法演示了如何应用allEntries CacheEvict(value employee, allEntries true)public void resetAllEntries() {// Intentionally blank} resetAllEntries()将CacheEvict的allEntries属性设置为“ true”并假定findEmployee(...)方法如下所示 Cacheable(value employee)public Person findEmployee(String firstName, String surname, int age) {return new Person(firstName, surname, age);} …然后在下面的代码resetAllEntries() 将清除“员工”缓存。 这意味着在JUnit测试中 employee1下面不会引用与employee2相同的对象 Testpublic void testCacheResetOfAllEntries() {Person employee1 instance.findEmployee(John, Smith, 22);instance.resetAllEntries();Person employee2 instance.findEmployee(John, Smith, 22);assertNotSame(employee1, employee2);} 第二个属性是beforeInvocation 。 这确定在调用方法之前或之后是否从缓存中清除数据项。 下面的代码非常荒谬。 但是它确实表明您可以同时将CacheEvict和Cacheable应用于方法。 CacheEvict(value employee, beforeInvocation true)Cacheable(value employee)public Person evictAndFindEmployee(String firstName, String surname, int age) {return new Person(firstName, surname, age);} 在上面的代码中 CacheEvict会在Cacheable搜索缓存之前使用匹配的键删除缓存中的所有条目。 由于Cacheable找不到任何条目因此它将调用我的代码将结果存储在缓存中。 对我的方法的后续调用将调用CacheEvict 它将删除任何适当的条目结果是在JUnit测试中变量下的employee1将永远不会引用与employee2相同的对象 Testpublic void testBeforeInvocation() {Person employee1 instance.evictAndFindEmployee(John, Smith, 22);Person employee2 instance.evictAndFindEmployee(John, Smith, 22);assertNotSame(employee1, employee2);} 就像我在上面说的那样由于我将Cacheable和CacheEvict都Cacheable同一方法 evictAndFindEmployee(...)似乎有点荒谬。 但是更重要的是它使代码不清楚并违反了单一责任原则。 因此我建议创建单独的可缓存和缓存退出方法。 例如如果您有一个缓存方法例如 Cacheable(value employee, key #surname)public Person findEmployeeBySurname(String firstName, String surname, int age) {return new Person(firstName, surname, age);} 然后假设您需要比简单的“清除所有”更好的缓存控制则可以轻松定义其对应项 CacheEvict(value employee, key #surname)public void resetOnSurname(String surname) {// Intentionally blank} 这是使用了已应用于同一规划环境地政司表达一个简单的空白标记方法Cacheable驱逐所有的Person 从其中关键的“姓”的说法相匹配的缓存实例。 Testpublic void testCacheResetOnSurname() {Person employee1 instance.findEmployeeBySurname(John, Smith, 22);instance.resetOnSurname(Smith);Person employee2 instance.findEmployeeBySurname(John, Smith, 22);assertNotSame(employee1, employee2);} 在上面的代码中对findEmployeeBySurname(...)的首次调用创建了一个Person对象Spring将其定义为“ Smith”的键存储在“员工”缓存中。 对resetOnSurname(...)的调用会清除“员工”缓存中所有姓为“ Smith”的条目最后第二次对findEmployeeBySurname(...)调用将创建一个新的Person对象Spring再次将其存储在“员工”缓存并带有“史密斯”键。 因此变量employee1和employee2没有引用相同的对象。 涵盖了Spring的缓存注释之后下一个难题是研究设置实用的缓存您如何启用Spring缓存以及应使用哪种缓存实现 稍后再说…… 祝您编程愉快别忘了分享 参考来自Captain Debug博客博客的JCG合作伙伴 Roger Hughes的Spring 3.1 Caching和CacheEvict 。 翻译自: https://www.javacodegeeks.com/2012/09/spring-31-caching-and-cacheevict.html