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..da41311a --- /dev/null +++ b/src/main/java/org/ayfaar/app/controllers/search/cache/CacheTable.java @@ -0,0 +1,34 @@ +package org.ayfaar.app.controllers.search.cache; + + +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; +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); + for (CacheEntity cacheEntity : cacheEntities) { + commonDao.remove(cacheEntity); + } + } + + @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 15ab7f1f..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,12 +12,18 @@ 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; +import java.net.URI; @Component +@Controller +@RequestMapping("api/dbcache") public class DBCache extends ConcurrentMapCache { @Inject CustomObjectMapper objectMapper; @Inject TermsMap termsMap; @@ -82,16 +88,30 @@ 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) { String name = ((ContentsCacheKey) key).categoryName; uid = commonDao.get(UID.class, UriGenerator.generate(Category.class, name)); } + if(uid != null) { commonDao.save(new CacheEntity(uid, json)); } super.put(key, json); } + + @RequestMapping("clear") + public void clearAll(){ + super.clear(); + } + + @RequestMapping("clearby/{uri}") + public void clearByURI(@PathVariable 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); + } + +}