Skip to content

Commit

Permalink
code to generate the file if missing #77
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteph-de committed Apr 12, 2024
1 parent 6c4fc09 commit f97a3bc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.mycore.common.config.annotation.MCRProperty;
import org.mycore.common.events.MCRShutdownHandler;
import org.mycore.jspdocportal.diskcache.disklru.DiskLruCache;
import org.mycore.jspdocportal.diskcache.disklru.DiskLruCache.Editor;
import org.mycore.jspdocportal.diskcache.disklru.DiskLruCache.Value;

public class MCRDiskcacheConfig {
Expand All @@ -24,9 +25,9 @@ public class MCRDiskcacheConfig {

private String id;
private Path baseDir;

//TODO "defaultName" in MCRInstance
@MCRInstance(name="Generator", valueClass=BiConsumer.class)
@MCRInstance(name = "Generator", valueClass = BiConsumer.class)
public BiConsumer<String, Path> generator;

@MCRProperty(name = "FileName", defaultName = "MCR.Diskcache.Default.FileName")
Expand Down Expand Up @@ -71,9 +72,12 @@ public void setVersion(String sVersion) {

@MCRPostConstruction
public void init(String property) {
id = property.substring(property.lastIndexOf(".") + 1);
String p = property.endsWith(".Class") ? property.substring(0, property.length() - 6) : property;
id = p.substring(p.lastIndexOf(".") + 1);
try {
cache = DiskLruCache.open(baseDir, version, DISK_LRUCACHE_VALUE_COUNT, maxSizeInBytes);
Path cacheDir = baseDir.resolve(id);
Files.createDirectories(cacheDir);
cache = DiskLruCache.open(cacheDir, version, DISK_LRUCACHE_VALUE_COUNT, maxSizeInBytes);
MCRShutdownHandler.getInstance().addCloseable(new MCRDiskLruCacheClosable(cache));
} catch (IOException e) {
LOGGER.error(e);
Expand Down Expand Up @@ -112,33 +116,50 @@ public DiskLruCache getCache() {
return cache;
}

private Value getFromCache(DiskLruCache cache, String key) {
private Value getFromCache(String key) {
try {
Value v = cache.get(key);
if (v != null) {
Path p = v.getFile(1);
Path p = v.getFile(0);
if (Files.getLastModifiedTime(p).toMillis() < System.currentTimeMillis() - livespanInMillis) {
cache.remove(key);
return null;
}
}
return v;
} catch (IOException e) {
LOGGER.error(e);
LOGGER.error(e);
}
return null;
}

public Path retrieveCachedFile(String objectId) {
Value v = getFromCache(cache, objectId);
Path p = v.getFile(0);
if (p == null) {
//TODO recreate FileObject and add it to Cache
public synchronized Path retrieveCachedFile(String objectId) {
Value v = getFromCache(objectId);
if (v != null && v.getFile(0) != null) {
return v.getFile(0);
} else {
//TODO Null-Check / Errorhandling
return generateCachedFile(objectId);
}
}

public Path generateCachedFile(String key) {
Editor editor = null;
Path p = null;
try {
editor = cache.edit(key);
p = editor.getFile(0);
generator.accept(key, p);
editor.commit();
} catch (IOException e) {
if (editor != null) {
editor.abortUnlessCommitted();
}
}
return p;
}

public void removeCachedFile(String objectId) {
public synchronized void removeCachedFile(String objectId) {
try {
cache.remove(objectId);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ MCR.Diskcache.Default.Version=1
MCR.Diskcache.Cache.iiif-manifest.Class=org.mycore.jspdocportal.diskcache.MCRDiskcacheConfig
MCR.Diskcache.Cache.iiif-manifest.FileName=/manifest
MCR.Diskcache.Cache.iiif-manifest.MimeType=application/json
MCR.Diskcache.Cache.iiif-manifest.Generator=org.mycore.jspdocportal.diskcache.generator.SimpleGenerator
MCR.Diskcache.Cache.iiif-manifest.Generator.Class=org.mycore.jspdocportal.diskcache.generator.SimpleGenerator

MCR.Diskcache.Cache.dv-mets.Class=org.mycore.jspdocportal.diskcache.MCRDiskcacheConfig
MCR.Diskcache.Cache.dv-mets.FileName=_dv.mets.xml
MCR.Diskcache.Cache.dv-mets.MimeType=application/xml
MCR.Diskcache.Cache.dv-mets.Generator=org.mycore.jspdocportal.diskcache.generator.SimpleGenerator
MCR.Diskcache.Cache.dv-mets.Generator.Class=org.mycore.jspdocportal.diskcache.generator.SimpleGenerator

MCR.Diskcache.Cache.hello.Class=org.mycore.jspdocportal.diskcache.MCRDiskcacheConfig
MCR.Diskcache.Cache.hello.FileName=/hello
MCR.Diskcache.Cache.hello.MimeType=application/text
MCR.Diskcache.Cache.hello.Generator=org.mycore.jspdocportal.diskcache.generator.SimpleGenerator
MCR.Diskcache.Cache.hello.Generator.Class=org.mycore.jspdocportal.diskcache.generator.SimpleGenerator

0 comments on commit f97a3bc

Please sign in to comment.