use of services.moleculer.util.CheckedTree in project moleculer-java by moleculer-java.
the class OHCacher method valueToBytes.
protected byte[] valueToBytes(Tree tree) throws Exception {
// Compress content
Tree root = new CheckedTree(Collections.singletonMap(CONTENT, tree.asObject()));
byte[] bytes = serializer.write(root);
boolean compressed;
if (compressAbove > 0 && bytes.length > compressAbove) {
bytes = compress(bytes, compressionLevel);
compressed = true;
} else {
compressed = false;
}
byte[] copy = new byte[bytes.length + 1];
System.arraycopy(bytes, 0, copy, 1, bytes.length);
if (compressed) {
// Compressed -> first byte = 1
copy[0] = (byte) 1;
}
return copy;
}
use of services.moleculer.util.CheckedTree in project moleculer-java by moleculer-java.
the class RedisCacher method set.
@Override
public Promise set(String key, Tree value, int ttl) {
if (status.get() == STATUS_CONNECTED) {
try {
SetArgs args;
if (ttl > 0) {
// Entry-level TTL (in seconds)
args = SetArgs.Builder.ex(ttl);
} else {
// Use the default TTL
args = expiration;
}
Tree root = new CheckedTree(Collections.singletonMap(CONTENT, value.asObject()));
return client.set(key, serializer.write(root), args);
} catch (Exception cause) {
logger.warn("Unable to put data into Redis!", cause);
}
}
return Promise.resolve();
}
use of services.moleculer.util.CheckedTree in project moleculer-java by moleculer-java.
the class JCacheCacher method set.
@Override
public Promise set(String key, Tree value, int ttl) {
try {
int pos = partitionPosition(key, true);
// Prefix is the name of the partition / region (eg.
// "user" from the "user.name" cache key)
String prefix = key.substring(0, pos);
javax.cache.Cache<String, byte[]> partition;
writeLock.lock();
try {
partition = partitions.get(prefix);
if (partition == null) {
partition = cacheManager.getCache(prefix, String.class, byte[].class);
if (partition == null) {
// Find partition-specific config
Configuration<String, byte[]> cfg = cacheConfigurations.get(prefix);
if (cfg == null) {
// Use default config
cfg = defaultConfiguration;
}
// Create new cache
partition = cacheManager.createCache(prefix, cfg);
}
partitions.put(prefix, partition);
}
} finally {
writeLock.unlock();
}
if (value == null) {
partition.remove(key);
} else {
Tree root = new CheckedTree(Collections.singletonMap(CONTENT, value.asObject()));
byte[] bytes = serializer.write(root);
partition.put(key.substring(pos + 1), bytes);
}
} catch (Throwable cause) {
logger.warn("Unable to write data to JCache!", cause);
}
return Promise.resolve();
}
Aggregations