001/*
002 * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.nuxeo.ecm.core.redis;
017
018import java.util.List;
019
020import redis.clients.jedis.Jedis;
021import redis.clients.jedis.JedisPubSub;
022import redis.clients.jedis.exceptions.JedisException;
023import redis.clients.util.Pool;
024
025/**
026 * Execute the jedis statement
027 *
028 * @since 6.0
029 */
030public interface RedisExecutor {
031
032    /**
033     * Loads the script into Redis.
034     *
035     * @return the script SHA1
036     * @since 8.10
037     */
038    String scriptLoad(String script) throws JedisException;
039
040    /**
041     * Evaluates the script of the given SHA1 with the given keys and arguments.
042     * <p>
043     * Can reload the script if the Redis instance restarted and the script isn't available anymore.
044     *
045     * @param sha1 the script SHA1
046     * @param keys the keys
047     * @param args the arguments
048     * @return the SHA1
049     * @since 8.10
050     */
051    Object evalsha(String sha1, List<String> keys, List<String> args) throws JedisException;
052
053    /**
054     * Evaluates the script of the given SHA1 with the given keys and arguments.
055     * <p>
056     * Can reload the script if the Redis instance restarted and the script isn't available anymore.
057     *
058     * @param sha1 the script SHA1
059     * @param keys the keys
060     * @param args the arguments
061     * @return the SHA1
062     * @since 8.10
063     */
064    Object evalsha(byte[] sha1, List<byte[]> keys, List<byte[]> args) throws JedisException;
065
066    <T> T execute(RedisCallable<T> call) throws JedisException;
067
068    /**
069     * Run a subscriber, do not return.
070     */
071    default void subscribe(JedisPubSub subscriber, String channel) throws JedisException {
072        execute(jedis -> {
073            jedis.subscribe(subscriber, channel);
074            return null;
075        });
076    }
077
078    /**
079     * Runs a subscriber to the given patterns.
080     *
081     * @param subscriber the subscriber
082     * @param patterns the channel patterns
083     * @since 9.1
084     */
085    default void psubscribe(JedisPubSub subscriber, String... patterns) throws JedisException {
086        execute(jedis -> {
087            jedis.psubscribe(subscriber, patterns);
088            return null;
089        });
090    }
091
092    Pool<Jedis> getPool();
093
094    /**
095     * Start to trace Redis activity only for debug purpose.
096     * @since 8.1
097     */
098    default void startMonitor() {
099    }
100
101    /**
102     * Stop tracing Redis activity.
103     * @since 8.1
104     */
105    default void stopMonitor() {
106    }
107
108}