javascript
Spring 3.1缓存和@CacheEvict
像@Cacheable一樣, @CacheEvict具有value , key和condition屬性。 它們的工作方式與@Cacheable支持的方式@Cacheable ,因此有關(guān)它們的更多信息,請參見我以前的博客: Spring 3.1 Caching和@Cacheable 。
CacheEvict支持兩個附加屬性: allEntries和beforeInvocation 。 如果我是一個賭博的人,我會花錢買最受歡迎的allEntries 。 allEntries用于完全清除@CacheEvict的強制value參數(shù)定義的高速緩存的內(nèi)容。 下面的方法演示了如何應(yīng)用allEntries :
@CacheEvict(value = "employee", allEntries = true)public void resetAllEntries() {// Intentionally blank}resetAllEntries()將@CacheEvict的allEntries屬性設(shè)置為“ 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 。 這確定在調(diào)用方法之前或之后是否從緩存中清除數(shù)據(jù)項。
下面的代碼非常荒謬。 但是,它確實表明您可以同時將@CacheEvict和@Cacheable應(yīng)用于方法。
@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找不到任何條目,因此它將調(diào)用我的代碼,將結(jié)果存儲在緩存中。 對我的方法的后續(xù)調(diào)用將調(diào)用@CacheEvict ,它將刪除任何適當(dāng)?shù)臈l目,結(jié)果是在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(...)似乎有點荒謬。 但是,更重要的是,它使代碼不清楚并違反了單一責(zé)任原則。 因此,我建議創(chuàng)建單獨的可緩存和緩存退出方法。 例如,如果您有一個緩存方法,例如:
@Cacheable(value = "employee", key = "#surname")public Person findEmployeeBySurname(String firstName, String surname, int age) {return new Person(firstName, surname, age);}然后,假設(shè)您需要比簡單的“清除所有”更好的緩存控制,則可以輕松定義其對應(yīng)項:
@CacheEvict(value = "employee", key = "#surname")public void resetOnSurname(String surname) {// Intentionally blank}這是使用了已應(yīng)用于同一規(guī)劃環(huán)境地政司表達一個簡單的空白標(biāo)記方法@Cacheable驅(qū)逐所有的Person ,從其中關(guān)鍵的“姓”的說法相匹配的緩存實例。
@Testpublic void testCacheResetOnSurname() {Person employee1 = instance.findEmployeeBySurname("John", "Smith", 22);instance.resetOnSurname("Smith");Person employee2 = instance.findEmployeeBySurname("John", "Smith", 22);assertNotSame(employee1, employee2);}在上面的代碼中,對findEmployeeBySurname(...)的首次調(diào)用創(chuàng)建了一個Person對象,Spring將其定義為“ Smith”的鍵存儲在“員工”緩存中。 對resetOnSurname(...)的調(diào)用會清除“員工”緩存中所有姓為“ Smith”的條目,最后第二次對findEmployeeBySurname(...)調(diào)用將創(chuàng)建一個新的Person對象,Spring再次將其存儲在“員工”緩存,并帶有“史密斯”鍵。 因此,變量employee1和employee2沒有引用相同的對象。
涵蓋了Spring的緩存注釋之后,下一個難題是研究設(shè)置實用的緩存:您如何啟用Spring緩存以及應(yīng)使用哪種緩存實現(xiàn)? 稍后再說……
祝您編程愉快,別忘了分享!
參考:來自Captain Debug博客博客的JCG合作伙伴 Roger Hughes的Spring 3.1 Caching和@CacheEvict 。
翻譯自: https://www.javacodegeeks.com/2012/09/spring-31-caching-and-cacheevict.html
總結(jié)
以上是生活随笔為你收集整理的Spring 3.1缓存和@CacheEvict的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linuxif语句(linux if 语
- 下一篇: Spring –持久层–编写实体并配置H