|
| 1 | +/* |
| 2 | + * Copyright 2019 Red Hat, Inc. |
| 3 | + * <p> |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | + * and Apache License v2.0 which accompanies this distribution. |
| 7 | + * <p> |
| 8 | + * The Eclipse Public License is available at |
| 9 | + * http://www.eclipse.org/legal/epl-v10.html |
| 10 | + * <p> |
| 11 | + * The Apache License v2.0 is available at |
| 12 | + * http://www.opensource.org/licenses/apache2.0.php |
| 13 | + * <p> |
| 14 | + * You may elect to redistribute this code under either of these licenses. |
| 15 | + */ |
| 16 | +package io.vertx.redis.client; |
| 17 | + |
| 18 | +import io.vertx.core.Future; |
| 19 | +import io.vertx.core.Handler; |
| 20 | +import io.vertx.core.Vertx; |
| 21 | +import io.vertx.redis.client.impl.CachingRedisClient; |
| 22 | +import io.vertx.redis.client.impl.cache.CacheKey; |
| 23 | + |
| 24 | +import java.util.Collection; |
| 25 | + |
| 26 | +/** |
| 27 | + * A {@link Redis} client wrapper that implements client-side caching. |
| 28 | + * |
| 29 | + * @see <a href="https://redis.io/docs/manual/client-side-caching/">Client-side caching in Redis</a> |
| 30 | + */ |
| 31 | +public interface CachingRedis extends Redis { |
| 32 | + |
| 33 | + /** |
| 34 | + * Create a new caching client using default client and cache options. |
| 35 | + * |
| 36 | + * @param vertx the vertx instance |
| 37 | + * @return the caching client |
| 38 | + */ |
| 39 | + static CachingRedis create(Vertx vertx) { |
| 40 | + return create(vertx, Redis.createClient(vertx)); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Create a new caching client wrapping an existing redis client with default caching options. |
| 45 | + * |
| 46 | + * @param vertx the vertx instance |
| 47 | + * @param redis the redis client to wrap |
| 48 | + * @return the caching client |
| 49 | + */ |
| 50 | + static CachingRedis create(Vertx vertx, Redis redis) { |
| 51 | + return create(vertx, redis, RedisClientCache.lru(new CachingRedisOptions())); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Create a new caching client using default client and cache options, backed by a given cache. |
| 56 | + * |
| 57 | + * @param vertx the vertx instance |
| 58 | + * @param cache the backing cache |
| 59 | + * @return the caching client |
| 60 | + */ |
| 61 | + static CachingRedis create(Vertx vertx, RedisClientCache cache) { |
| 62 | + return create(vertx, Redis.createClient(vertx), cache); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Create a new caching client wrapping an existing redis client and backed by a given cache. |
| 67 | + * |
| 68 | + * @param vertx the vertx instance |
| 69 | + * @param redis the redis client to wrap |
| 70 | + * @param cache the backing cache |
| 71 | + * @return the caching client |
| 72 | + */ |
| 73 | + static CachingRedis create(Vertx vertx, Redis redis, RedisClientCache cache) { |
| 74 | + return create(vertx, redis, cache, new CachingRedisOptions()); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Create a new caching client wrapping an existing redis client and using the given cache options. |
| 79 | + * |
| 80 | + * @param vertx the vertx instance |
| 81 | + * @param redis the redis client to wrap |
| 82 | + * @param options the cache options |
| 83 | + * @return the caching client |
| 84 | + */ |
| 85 | + static CachingRedis create(Vertx vertx, Redis redis, CachingRedisOptions options) { |
| 86 | + return create(vertx, redis, RedisClientCache.lru(options), options); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Create a new caching client wrapping an existing redis client, using the given cache and cache options. |
| 91 | + * |
| 92 | + * @param vertx the vertx instance |
| 93 | + * @param redis the redis client to wrap |
| 94 | + * @param cache the backing cache |
| 95 | + * @param options the cache options |
| 96 | + * @return the caching client |
| 97 | + */ |
| 98 | + static CachingRedis create(Vertx vertx, Redis redis, RedisClientCache cache, CachingRedisOptions options) { |
| 99 | + return new CachingRedisClient(vertx, redis, cache, options); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Flush the local cache. |
| 104 | + * |
| 105 | + * <p> |
| 106 | + * This operation only clears the local cache and has no interaction with the server. |
| 107 | + * |
| 108 | + * @return a future indicating the status of the operation |
| 109 | + */ |
| 110 | + Future<Void> flush(); |
| 111 | + |
| 112 | + /** |
| 113 | + * Set a handler to be called when invalidation is performed. |
| 114 | + * |
| 115 | + * <p> |
| 116 | + * The client will clear the keys before this handler is invoked. It is not recommended to modify |
| 117 | + * the cache as a part of this handler. The primary function is for instrumentation. |
| 118 | + * |
| 119 | + * @param handler a handler that accepts the keys which were invalidated |
| 120 | + * @return fluent self |
| 121 | + */ |
| 122 | + CachingRedis invalidationHandler(Handler<Collection<CacheKey>> handler); |
| 123 | +} |
0 commit comments