Class MemoryKeyValueStore
java.lang.Object
com.codename1.backend.kv.MemoryKeyValueStore
- All Implemented Interfaces:
KeyValueStore, Closeable, AutoCloseable
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 -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this and releases what it holds.booleancompareAndSet(String key, byte[] expected, byte[] value, long ttlMillis) Replaces the value underkeyonly while it still equalsexpected(null: while there is none); answers whether it did.booleanRemoveskey; answers whether it existed.intdeleteByPrefix(String prefix) Removes every key that starts withprefix; answers how many.byte[]The value underkey, or null when there is none or it expired.longAddsdeltato the integer underkeyand answers the result; a missing key counts from zero and is giventtlMillis(zero or less: none).booleanisShared()Whether this store is shared with other processes.booleanping()Whether the store answers right now; what a health check asks.longSendsmessageto everybody subscribed tochannel, on every instance; answers how many subscribers received it (on Redis, how many connections).voidStoresvalueunderkey, replacing what was there.booleansetIfAbsent(String key, byte[] value, long ttlMillis) Storesvalueonly ifkeyholds nothing; answers whether it did.subscribe(String channel, KeyValueStore.MessageListener listener) Callslistenerfor every message published tochanneluntil the returned subscription is closed.longHow many millisecondskeyhas left: -1 when it never expires, -2 when it does not exist.
-
Constructor Details
-
MemoryKeyValueStore
public MemoryKeyValueStore()
-
-
Method Details
-
get
Description copied from interface:KeyValueStoreThe value underkey, or null when there is none or it expired.- Specified by:
getin interfaceKeyValueStore- Throws:
IOException
-
set
Description copied from interface:KeyValueStoreStores
valueunderkey, replacing what was there.ttlMillis: how long it lives; zero or less keeps it until it is deleted
- Specified by:
setin interfaceKeyValueStore- Throws:
IOException
-
setIfAbsent
Description copied from interface:KeyValueStoreStoresvalueonly ifkeyholds nothing; answers whether it did. This is the primitive a lock is built on.- Specified by:
setIfAbsentin interfaceKeyValueStore- Throws:
IOException
-
compareAndSet
public boolean compareAndSet(String key, byte[] expected, byte[] value, long ttlMillis) throws IOException Description copied from interface:KeyValueStoreReplaces the value underkeyonly while it still equalsexpected(null: while there is none); answers whether it did. A nullvaluedeletes the key instead. What an optimistic read-modify-write loops on.- Specified by:
compareAndSetin interfaceKeyValueStore- Throws:
IOException
-
delete
Description copied from interface:KeyValueStoreRemoveskey; answers whether it existed.- Specified by:
deletein interfaceKeyValueStore- Throws:
IOException
-
deleteByPrefix
Description copied from interface:KeyValueStoreRemoves every key that starts withprefix; 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:
deleteByPrefixin interfaceKeyValueStore- Throws:
IOException
-
increment
Description copied from interface:KeyValueStoreAddsdeltato the integer underkeyand answers the result; a missing key counts from zero and is giventtlMillis(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:
incrementin interfaceKeyValueStore- Throws:
IOException
-
ttl
Description copied from interface:KeyValueStoreHow many millisecondskeyhas left: -1 when it never expires, -2 when it does not exist.- Specified by:
ttlin interfaceKeyValueStore- Throws:
IOException
-
publish
Description copied from interface:KeyValueStoreSendsmessageto everybody subscribed tochannel, 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:
publishin interfaceKeyValueStore- Throws:
IOException
-
subscribe
public KeyValueStore.Subscription subscribe(String channel, KeyValueStore.MessageListener listener) throws IOException Description copied from interface:KeyValueStoreCallslistenerfor every message published tochanneluntil 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:
subscribein interfaceKeyValueStore- Throws:
IOException
-
ping
public boolean ping()Description copied from interface:KeyValueStoreWhether the store answers right now; what a health check asks.- Specified by:
pingin interfaceKeyValueStore
-
close
public void close()Description copied from interface:CloseableCloses this and releases what it holds. Closing it again has no effect.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-