Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue111 #133

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<CacheEntity> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}


}
31 changes: 31 additions & 0 deletions src/test/java/issues/issue111/Issue111UnitTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}