All Downloads are FREE. Search and download functionalities are using the official Maven repository.

es.moki.ratelimitj.redis.RedisScriptLoader Maven / Gradle / Ivy

There is a newer version: 0.7.0
Show newest version
package es.moki.ratelimitj.redis;


import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import com.lambdaworks.redis.api.StatefulRedisConnection;

import java.io.IOException;
import java.net.URL;

import static java.util.Objects.requireNonNull;

public class RedisScriptLoader {

    private final StatefulRedisConnection connection;
    private final String scriptUri;
    private volatile String shaInstance;

    public RedisScriptLoader(StatefulRedisConnection connection, String scriptUri) {
        this(connection, scriptUri, false);
    }

    public RedisScriptLoader(StatefulRedisConnection connection, String scriptUri, boolean eagerLoad) {
        requireNonNull(connection);
        this.connection = connection;
        this.scriptUri = requireNonNull(scriptUri);
        if (eagerLoad) {
            scriptSha();
        }
    }

    String scriptSha() {
        // safe local double-checked locking
        // http://shipilev.net/blog/2014/safe-public-construction/
        String sha = shaInstance;
        if (sha == null) {
            synchronized (this) {
                sha = shaInstance;
                if (sha == null) {
                    sha = loadScript();
                    shaInstance = sha;
                }
            }
        }
        return sha;
    }

    private String loadScript() {
        String script;
        try {
            script = readScriptFile();
        } catch (IOException e) {
            throw new RuntimeException("Unable to load Redis LUA script file", e);
        }

        return connection.sync().scriptLoad(script);
    }

    private String readScriptFile() throws IOException {
        // TODO remove guava depedency - java file loading is a mess!
        URL url = Resources.getResource(scriptUri);
        return Resources.toString(url, Charsets.UTF_8);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy