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

com.twitter.finagle.memcached.java.Client Maven / Gradle / Ivy

There is a newer version: 21.2.0
Show newest version
package com.twitter.finagle.memcached.java;

import java.util.List;
import java.util.Map;

import com.twitter.finagle.Service;
import com.twitter.finagle.memcached.protocol.Command;
import com.twitter.finagle.memcached.protocol.Response;
import com.twitter.io.Buf;
import com.twitter.util.Future;
import com.twitter.util.Time;

/**
 * A Java-friendly memcached client.
 */
public abstract class Client {
  /**
   * Construct a Client from a single Service
   *
   * @param finagleClient a Service
   * @return a Client.
   */
  public static Client newInstance(Service finagleClient) {
    com.twitter.finagle.memcached.Client schmemcachedClient =
      com.twitter.finagle.memcached.Client$.MODULE$.apply(finagleClient);
    return new com.twitter.finagle.memcached.java.ClientBase(schmemcachedClient);
  }

  /**
   * Get a key from the server.
   */
  public abstract Future get(String key);

  /**
   * Get a key from the server together with a "cas unique" token used
   * in cas operations.
   */
  public abstract Future gets(String key);

  /**
   * Get a set of keys from the server.
   * @return a Map[String, Buf] of all of the keys that the server had.
   */
  public abstract Future> get(List keys);

  /**
   * Get a set of keys from the server together with a "cas unique" token.
   * @return a Map[String, ResultWithCAS] of all of the keys that the server had.
   */
  public abstract Future> gets(List keys);

  /**
   * Store a key. Override an existing value.
   * @return true
   */
  public abstract Future set(String key, Buf value);

  /**
   * Store a key. Override an existing value.
   * @return void
   */
  public abstract Future set(String key, int flags, Time expiry, Buf value);

  /**
   * Store a key but only if it doesn't already exist on the server.
   * @return true if stored, false if not stored
   */
  public abstract Future add(String key, Buf value);

  /**
   * Store a key but only if it doesn't already exist on the server.
   * @return true if stored, false if not stored
   */
  public abstract Future add(String key, int flags, Time expiry, Buf value);

  /**
   * Append bytes to the end of an existing key. If the key doesn't
   * exist, the operation has no effect.
   * @return true if stored, false if not stored
   */
  public abstract Future append(String key, Buf value);

  /**
   * Prepend bytes to the beginning of an existing key. If the key
   * doesn't exist, the operation has no effect.
   * @return true if stored, false if not stored
   */
  public abstract Future prepend(String key, Buf value);

  /**
   * Replace bytes on an existing key. If the key doesn't exist, the
   * operation has no effect.
   * @return true if stored, false if not stored
   */
  public abstract Future replace(String key, Buf value);

  /**
   * Replace bytes on an existing key. If the key doesn't exist, the
   * operation has no effect.
   * @return true if stored, false if not stored
   */
  public abstract Future replace(String key, int flags, Time expiry, Buf value);

 /**
   * Perform a compare-and-set operation.  This is like a replace,
   * except a token identifying the value version to replace is
   * provided.  Tokens are retrieved with "gets"
   *
   * @return true if stored, false if not stored
   */

  public abstract Future cas(
    String key, int flags, Time expiry,
    Buf value, Buf casUnique);

  /**
   * A version of cas with default flags & expiry paramters.
   */
  public abstract Future cas(String key, Buf value, Buf casUnique);

  /**
   * Convenience version of cas used to store string values.
   */
  public Future cas(String key, String value, Buf casUnique) {
    return this.cas(key, toBuffer(value), casUnique);
  }

  /**
   * Remove a key.
   * @return true if deleted, false if not found
   */
  public abstract Future delete(String key);

  /**
   * Increment a key. Interpret the value as an Long if it is parsable.
   * This operation has no effect if there is no value there already.
   */
  public abstract Future incr(String key);
  public abstract Future incr(String key, long delta);

  /**
   * Decrement a key. Interpret the value as an Long if it is parsable.
   * This operation has no effect if there is no value there already.
   */
  public abstract Future decr(String key);
  public abstract Future decr(String key, long delta);

  public Future set(String key, String value) {
    return this.set(key, toBuffer(value));
  }

  public Future add(String key, String value) {
    return this.add(key, toBuffer(value));
  }

  public Future append(String key, String value) {
    return this.append(key, toBuffer(value));
  }

  public Future prepend(String key, String value) {
    return this.prepend(key, toBuffer(value));
  }

  /**
   * release the underlying service(s)
   */
  public abstract void release();

  private Buf toBuffer(String value) {
    return Buf.Utf8$.MODULE$.apply(value);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy