Search in sources :

Example 1 with CheckedTree

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;
}
Also used : CheckedTree(services.moleculer.util.CheckedTree) CheckedTree(services.moleculer.util.CheckedTree) Tree(io.datatree.Tree)

Example 2 with CheckedTree

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();
}
Also used : CheckedTree(services.moleculer.util.CheckedTree) CheckedTree(services.moleculer.util.CheckedTree) Tree(io.datatree.Tree) SetArgs(com.lambdaworks.redis.SetArgs)

Example 3 with CheckedTree

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();
}
Also used : CheckedTree(services.moleculer.util.CheckedTree) CheckedTree(services.moleculer.util.CheckedTree) Tree(io.datatree.Tree)

Aggregations

Tree (io.datatree.Tree)3 CheckedTree (services.moleculer.util.CheckedTree)3 SetArgs (com.lambdaworks.redis.SetArgs)1