This repository was archived by the owner on Apr 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc518ee
commit 24c0153
Showing
9 changed files
with
156 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/main/java/io/rudin/minetest/tileserver/service/impl/EHTileCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package io.rudin.minetest.tileserver.service.impl; | ||
|
||
import io.rudin.minetest.tileserver.config.TileServerConfig; | ||
import io.rudin.minetest.tileserver.service.TileCache; | ||
import org.ehcache.Cache; | ||
import org.ehcache.PersistentCacheManager; | ||
import org.ehcache.config.builders.CacheConfigurationBuilder; | ||
import org.ehcache.config.builders.CacheManagerBuilder; | ||
import org.ehcache.config.builders.ResourcePoolsBuilder; | ||
import org.ehcache.config.units.EntryUnit; | ||
import org.ehcache.config.units.MemoryUnit; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
@Singleton | ||
public class EHTileCache implements TileCache { | ||
|
||
@Inject | ||
public EHTileCache(TileServerConfig cfg){ | ||
this.cfg = cfg; | ||
this.timestampMarker = new File(cfg.tileDirectory(), "timestampmarker-ehcache"); | ||
|
||
if (!timestampMarker.isFile()){ | ||
try (OutputStream output = new FileOutputStream(timestampMarker)){ | ||
output.write(0x00); | ||
} catch (Exception e){ | ||
throw new IllegalArgumentException("could not create timestamp marker!", e); | ||
} | ||
|
||
timestampMarker.setLastModified(0); | ||
} | ||
|
||
|
||
PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder() | ||
.with(CacheManagerBuilder.persistence(new File(cfg.tileDirectory(), "ehcache"))) | ||
.withCache("cache", | ||
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, byte[].class, | ||
ResourcePoolsBuilder.newResourcePoolsBuilder() | ||
.heap(10, MemoryUnit.MB) | ||
.offheap(50, MemoryUnit.MB) | ||
.disk(10, MemoryUnit.GB, true) | ||
) | ||
).build(true); | ||
|
||
cache = persistentCacheManager.getCache("cache", String.class, byte[].class); | ||
|
||
Runtime.getRuntime().addShutdownHook(new Thread(persistentCacheManager::close)); | ||
} | ||
|
||
private final File timestampMarker; | ||
|
||
private final TileServerConfig cfg; | ||
|
||
private final Cache<String, byte[]> cache; | ||
|
||
private String getKey(int x, int y, int z){ | ||
//TODO: long key | ||
return x + "/" + y + "/" + z; | ||
} | ||
|
||
|
||
@Override | ||
public void put(int x, int y, int z, byte[] data) throws IOException { | ||
cache.put(getKey(x,y,z), data); | ||
timestampMarker.setLastModified(System.currentTimeMillis()); | ||
} | ||
|
||
@Override | ||
public byte[] get(int x, int y, int z) throws IOException { | ||
return cache.get(getKey(x,y,z)); | ||
} | ||
|
||
@Override | ||
public boolean has(int x, int y, int z) { | ||
return cache.containsKey(getKey(x,y,z)); | ||
} | ||
|
||
@Override | ||
public void remove(int x, int y, int z) { | ||
cache.remove(getKey(x,y,z)); | ||
} | ||
|
||
@Override | ||
public long getLatestTimestamp() { | ||
return timestampMarker.lastModified(); | ||
} | ||
} |