From 086e1e05435ad9cc6a146a2ea9b73f355e7d7fb5 Mon Sep 17 00:00:00 2001 From: Yurii Date: Mon, 8 Dec 2014 19:36:16 +0200 Subject: [PATCH 1/6] add clear method --- .../ayfaar/app/controllers/search/cache/DBCache.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java b/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java index 15ab7f1f..460b587a 100644 --- a/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java +++ b/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java @@ -15,6 +15,7 @@ import javax.inject.Inject; import java.io.IOException; +import java.net.URI; @Component @@ -94,4 +95,13 @@ public void put(Object key, Object value) { } super.put(key, json); } + + public void clearAll(){ + super.clear(); + } + + public void clearByURI(URI uri){ + super.evict(uri.toString()); + } + } From 4d302ba6150679c4f4c479e0597124dab6864223 Mon Sep 17 00:00:00 2001 From: Yurii Date: Wed, 10 Dec 2014 11:20:20 +0200 Subject: [PATCH 2/6] little fix --- .../java/org/ayfaar/app/controllers/search/cache/DBCache.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java b/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java index 460b587a..deed7020 100644 --- a/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java +++ b/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java @@ -83,7 +83,7 @@ public void put(Object key, Object value) { if (key instanceof SearchCacheKey && ((SearchCacheKey) key).page == 0) { TermsMap.TermProvider provider = termsMap.getTermProvider(((SearchCacheKey) key).query); - if(provider != null && ((SearchCacheKey) key).page == 0) { + if(provider != null) { uid = provider.hasMainTerm() ? provider.getMainTermProvider().getTerm() : provider.getTerm(); } } else if(key instanceof ContentsCacheKey) { @@ -101,7 +101,7 @@ public void clearAll(){ } public void clearByURI(URI uri){ - super.evict(uri.toString()); + super.evict(uri); } } From 1caa26f4fbdecc559ccc6d4d608e971faa5826da Mon Sep 17 00:00:00 2001 From: Yurii Date: Wed, 10 Dec 2014 12:16:08 +0200 Subject: [PATCH 3/6] add clear by uri --- .../org/ayfaar/app/controllers/search/cache/DBCache.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java b/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java index deed7020..94459afd 100644 --- a/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java +++ b/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java @@ -90,6 +90,7 @@ public void put(Object key, Object value) { String name = ((ContentsCacheKey) key).categoryName; uid = commonDao.get(UID.class, UriGenerator.generate(Category.class, name)); } + if(uid != null) { commonDao.save(new CacheEntity(uid, json)); } @@ -100,8 +101,11 @@ public void clearAll(){ super.clear(); } - public void clearByURI(URI uri){ - super.evict(uri); + public void clearByURI(String uri){ + + SearchCacheKey searchKey = new SearchCacheKey(uri,0); + super.evict(searchKey); } + } From 699d365a134c071b7059cd0e98dfe2c860f18c16 Mon Sep 17 00:00:00 2001 From: Yurii Date: Wed, 10 Dec 2014 13:51:51 +0200 Subject: [PATCH 4/6] add some test and work with CacheTable --- .../controllers/search/cache/CacheTable.java | 26 ++++++++++++++++ .../app/controllers/search/cache/DBCache.java | 1 - .../issues/issue111/Issue111UnitTest.java | 31 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/ayfaar/app/controllers/search/cache/CacheTable.java create mode 100644 src/test/java/issues/issue111/Issue111UnitTest.java diff --git a/src/main/java/org/ayfaar/app/controllers/search/cache/CacheTable.java b/src/main/java/org/ayfaar/app/controllers/search/cache/CacheTable.java new file mode 100644 index 00000000..5f4309cb --- /dev/null +++ b/src/main/java/org/ayfaar/app/controllers/search/cache/CacheTable.java @@ -0,0 +1,26 @@ +package org.ayfaar.app.controllers.search.cache; + + +import org.ayfaar.app.dao.CommonDao; + +import javax.inject.Inject; +import java.util.List; + +public class CacheTable { + + @Inject CommonDao commonDao; + + public void clearCacheTable(){ + + List cacheEntities = commonDao.getAll(CacheEntity.class); + for (CacheEntity cacheEntity : cacheEntities) { + commonDao.remove(cacheEntity); + } + } + + public void clearByURI(String uri){ + + commonDao.remove(CacheEntity.class,uri); + } + +} diff --git a/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java b/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java index 94459afd..c3e586ef 100644 --- a/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java +++ b/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java @@ -102,7 +102,6 @@ public void clearAll(){ } public void clearByURI(String uri){ - SearchCacheKey searchKey = new SearchCacheKey(uri,0); super.evict(searchKey); } diff --git a/src/test/java/issues/issue111/Issue111UnitTest.java b/src/test/java/issues/issue111/Issue111UnitTest.java new file mode 100644 index 00000000..27901520 --- /dev/null +++ b/src/test/java/issues/issue111/Issue111UnitTest.java @@ -0,0 +1,31 @@ +package issues.issue111; + +import org.ayfaar.app.controllers.search.cache.CacheEntity; +import org.ayfaar.app.controllers.search.cache.CacheTable; +import org.ayfaar.app.controllers.search.cache.DBCache; +import org.ayfaar.app.dao.CommonDao; +import org.junit.Assert; +import org.junit.Test; + +import javax.inject.Inject; + +public class Issue111UnitTest { + + @Inject + DBCache dbCache; + @Inject + CommonDao commonDao; + + @Test + public void ClearDBCache(){ + dbCache.clearAll(); + Assert.assertEquals(dbCache, new DBCache()); + } + + public void ClearCachTable(){ + CacheTable cacheTable = new CacheTable(); + cacheTable.clearCacheTable(); + Assert.assertEquals(commonDao.getAll(CacheEntity.class),0); + } + +} From efd0668693ee3429a6bf775bf7b0c2a6a7512bc9 Mon Sep 17 00:00:00 2001 From: Yurii Date: Wed, 10 Dec 2014 17:16:29 +0200 Subject: [PATCH 5/6] add url --- .../org/ayfaar/app/controllers/search/cache/CacheTable.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/org/ayfaar/app/controllers/search/cache/CacheTable.java b/src/main/java/org/ayfaar/app/controllers/search/cache/CacheTable.java index 5f4309cb..832e2152 100644 --- a/src/main/java/org/ayfaar/app/controllers/search/cache/CacheTable.java +++ b/src/main/java/org/ayfaar/app/controllers/search/cache/CacheTable.java @@ -2,14 +2,20 @@ import org.ayfaar.app.dao.CommonDao; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; import javax.inject.Inject; import java.util.List; +@Controller +@RequestMapping("api/cache") public class CacheTable { @Inject CommonDao commonDao; + + @RequestMapping("clear") public void clearCacheTable(){ List cacheEntities = commonDao.getAll(CacheEntity.class); From 350e38e733eabe4eb08a13aaccf39bdc14fced34 Mon Sep 17 00:00:00 2001 From: Yurii Date: Wed, 10 Dec 2014 18:04:27 +0200 Subject: [PATCH 6/6] add url --- .../ayfaar/app/controllers/search/cache/CacheTable.java | 4 +++- .../org/ayfaar/app/controllers/search/cache/DBCache.java | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/ayfaar/app/controllers/search/cache/CacheTable.java b/src/main/java/org/ayfaar/app/controllers/search/cache/CacheTable.java index 832e2152..da41311a 100644 --- a/src/main/java/org/ayfaar/app/controllers/search/cache/CacheTable.java +++ b/src/main/java/org/ayfaar/app/controllers/search/cache/CacheTable.java @@ -3,6 +3,7 @@ import org.ayfaar.app.dao.CommonDao; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import javax.inject.Inject; @@ -24,7 +25,8 @@ public void clearCacheTable(){ } } - public void clearByURI(String uri){ + @RequestMapping("clearby/{uri}") + public void clearByURI(@PathVariable String uri){ commonDao.remove(CacheEntity.class,uri); } diff --git a/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java b/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java index c3e586ef..78f1a436 100644 --- a/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java +++ b/src/main/java/org/ayfaar/app/controllers/search/cache/DBCache.java @@ -12,6 +12,9 @@ import org.springframework.cache.support.SimpleValueWrapper; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; import javax.inject.Inject; import java.io.IOException; @@ -19,6 +22,8 @@ @Component +@Controller +@RequestMapping("api/dbcache") public class DBCache extends ConcurrentMapCache { @Inject CustomObjectMapper objectMapper; @Inject TermsMap termsMap; @@ -97,11 +102,13 @@ public void put(Object key, Object value) { super.put(key, json); } + @RequestMapping("clear") public void clearAll(){ super.clear(); } - public void clearByURI(String uri){ + @RequestMapping("clearby/{uri}") + public void clearByURI(@PathVariable String uri){ SearchCacheKey searchKey = new SearchCacheKey(uri,0); super.evict(searchKey); }