Class MemoryKeyValueStore

java.lang.Object
com.codename1.backend.kv.MemoryKeyValueStore
All Implemented Interfaces:
KeyValueStore, Closeable, AutoCloseable

public final class MemoryKeyValueStore extends Object implements KeyValueStore

The process-local KeyValueStore: what cn1.kv.url=memory: and an unconfigured server get.

Everything a shared store does works here too -- expiry, compare-and-set, counters and pub/sub -- within this process, so an application developed against it behaves the same when Redis is configured, minus the sharing. Expired entries are dropped when touched and by a sweep every 1024 writes.

  • Nested Class Summary

    Nested classes/interfaces inherited from interface KeyValueStore

    KeyValueStore.MessageListener, KeyValueStore.Subscription
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes this and releases what it holds.
    boolean
    compareAndSet(String key, byte[] expected, byte[] value, long ttlMillis)
    Replaces the value under key only while it still equals expected (null: while there is none); answers whether it did.
    boolean
    Removes key; answers whether it existed.
    int
    Removes every key that starts with prefix; answers how many.
    byte[]
    get(String key)
    The value under key, or null when there is none or it expired.
    long
    increment(String key, long delta, long ttlMillis)
    Adds delta to the integer under key and answers the result; a missing key counts from zero and is given ttlMillis (zero or less: none).
    boolean
    Whether this store is shared with other processes.
    boolean
    Whether the store answers right now; what a health check asks.
    long
    publish(String channel, byte[] message)
    Sends message to everybody subscribed to channel, on every instance; answers how many subscribers received it (on Redis, how many connections).
    void
    set(String key, byte[] value, long ttlMillis)
    Stores value under key, replacing what was there.
    boolean
    setIfAbsent(String key, byte[] value, long ttlMillis)
    Stores value only if key holds nothing; answers whether it did.
    Calls listener for every message published to channel until the returned subscription is closed.
    long
    ttl(String key)
    How many milliseconds key has left: -1 when it never expires, -2 when it does not exist.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MemoryKeyValueStore

      public MemoryKeyValueStore()
  • Method Details

    • get

      public byte[] get(String key) throws IOException
      Description copied from interface: KeyValueStore
      The value under key, or null when there is none or it expired.
      Specified by:
      get in interface KeyValueStore
      Throws:
      IOException
    • set

      public void set(String key, byte[] value, long ttlMillis) throws IOException
      Description copied from interface: KeyValueStore

      Stores value under key, replacing what was there.

      • ttlMillis: how long it lives; zero or less keeps it until it is deleted
      Specified by:
      set in interface KeyValueStore
      Throws:
      IOException
    • setIfAbsent

      public boolean setIfAbsent(String key, byte[] value, long ttlMillis) throws IOException
      Description copied from interface: KeyValueStore
      Stores value only if key holds nothing; answers whether it did. This is the primitive a lock is built on.
      Specified by:
      setIfAbsent in interface KeyValueStore
      Throws:
      IOException
    • compareAndSet

      public boolean compareAndSet(String key, byte[] expected, byte[] value, long ttlMillis) throws IOException
      Description copied from interface: KeyValueStore
      Replaces the value under key only while it still equals expected (null: while there is none); answers whether it did. A null value deletes the key instead. What an optimistic read-modify-write loops on.
      Specified by:
      compareAndSet in interface KeyValueStore
      Throws:
      IOException
    • delete

      public boolean delete(String key) throws IOException
      Description copied from interface: KeyValueStore
      Removes key; answers whether it existed.
      Specified by:
      delete in interface KeyValueStore
      Throws:
      IOException
    • deleteByPrefix

      public int deleteByPrefix(String prefix) throws IOException
      Description copied from interface: KeyValueStore
      Removes every key that starts with prefix; answers how many. Walks the key space incrementally on a shared server rather than blocking it, so keys written while it runs may survive.
      Specified by:
      deleteByPrefix in interface KeyValueStore
      Throws:
      IOException
    • increment

      public long increment(String key, long delta, long ttlMillis) throws IOException
      Description copied from interface: KeyValueStore
      Adds delta to the integer under key and answers the result; a missing key counts from zero and is given ttlMillis (zero or less: none). The expiry of an existing key is left as it is, so a fixed window counts from its first request.
      Specified by:
      increment in interface KeyValueStore
      Throws:
      IOException
    • ttl

      public long ttl(String key) throws IOException
      Description copied from interface: KeyValueStore
      How many milliseconds key has left: -1 when it never expires, -2 when it does not exist.
      Specified by:
      ttl in interface KeyValueStore
      Throws:
      IOException
    • publish

      public long publish(String channel, byte[] message) throws IOException
      Description copied from interface: KeyValueStore
      Sends message to everybody subscribed to channel, on every instance; answers how many subscribers received it (on Redis, how many connections). Delivery is at most once: a subscriber that is not connected at that moment never sees it.
      Specified by:
      publish in interface KeyValueStore
      Throws:
      IOException
    • subscribe

      public KeyValueStore.Subscription subscribe(String channel, KeyValueStore.MessageListener listener) throws IOException
      Description copied from interface: KeyValueStore
      Calls listener for every message published to channel until the returned subscription is closed. The listener runs on the store's own receiving thread, one message at a time; it should hand slow work off.
      Specified by:
      subscribe in interface KeyValueStore
      Throws:
      IOException
    • ping

      public boolean ping()
      Description copied from interface: KeyValueStore
      Whether the store answers right now; what a health check asks.
      Specified by:
      ping in interface KeyValueStore
    • isShared

      public boolean isShared()
      Description copied from interface: KeyValueStore
      Whether this store is shared with other processes. A local store's locks and counters are only good within one process.
      Specified by:
      isShared in interface KeyValueStore
    • close

      public void close()
      Description copied from interface: Closeable
      Closes this and releases what it holds. Closing it again has no effect.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable