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    public static final RedisExecutor NOOP = new RedisAbstractExecutor() {
033
034        @Override
035        public <T> T execute(RedisCallable<T> call) throws JedisException {
036            throw new UnsupportedOperationException("No redis executor available");
037        }
038
039        @Override
040        public Pool<Jedis> getPool() {
041            throw new UnsupportedOperationException("No pool available");
042        }
043
044    };
045
046    /**
047     * Loads the script into Redis.
048     *
049     * @return the script SHA1
050     * @since 8.10
051     */
052    String scriptLoad(String script) throws JedisException;
053
054    /**
055     * Evaluates the script of the given SHA1 with the given keys and arguments.
056     * <p>
057     * Can reload the script if the Redis instance restarted and the script isn't available anymore.
058     *
059     * @param sha1 the script SHA1
060     * @param keys the keys
061     * @param args the arguments
062     * @return the SHA1
063     * @since 8.10
064     */
065    Object evalsha(String sha1, List<String> keys, List<String> args) throws JedisException;
066
067    /**
068     * Evaluates the script of the given SHA1 with the given keys and arguments.
069     * <p>
070     * Can reload the script if the Redis instance restarted and the script isn't available anymore.
071     *
072     * @param sha1 the script SHA1
073     * @param keys the keys
074     * @param args the arguments
075     * @return the SHA1
076     * @since 8.10
077     */
078    Object evalsha(byte[] sha1, List<byte[]> keys, List<byte[]> args) throws JedisException;
079
080    <T> T execute(RedisCallable<T> call) throws JedisException;
081
082    /**
083     * Run a subscriber, do not return.
084     */
085    default void subscribe(JedisPubSub subscriber, String channel) throws JedisException {
086        execute(jedis -> {
087            jedis.subscribe(subscriber, channel);
088            return null;
089        });
090    }
091
092    /**
093     * Runs a subscriber to the given patterns.
094     *
095     * @param subscriber the subscriber
096     * @param patterns the channel patterns
097     * @since 9.1
098     */
099    default void psubscribe(JedisPubSub subscriber, String... patterns) throws JedisException {
100        execute(jedis -> {
101            jedis.psubscribe(subscriber, patterns);
102            return null;
103        });
104    }
105
106    Pool<Jedis> getPool();
107
108    /**
109     * Start to trace Redis activity only for debug purpose.
110     * @since 8.1
111     */
112    default void startMonitor() {
113    }
114
115    /**
116     * Stop tracing Redis activity.
117     * @since 8.1
118     */
119    default void stopMonitor() {
120    }
121
122}