[id='jcache-store-retrieve-data_{context}']
= Store and retrieve data

Even though JCache API does not extend neither
link:{jdkdocroot}/java/util/Map.html[java.util.Map]
not link:{jdkdocroot}/java/util/concurrent/ConcurrentMap.html[java.util.concurrent.ConcurrentMap],
it providers a key/value API to store and retrieve data:

[source,java]
----
include::code_examples/JCacheStoreRetrieve.java[]
----

Contrary to standard link:{jdkdocroot}/java/util/Map.html[java.util.Map],
link:https://github.com/jsr107/jsr107spec/blob/v1.0.0-RC1/src/main/java/javax/cache/Cache.java[javax.cache.Cache]
comes with two basic put methods called put and getAndPut. The former returns
`void` whereas the latter returns the previous value associated with the key.
So, the equivalent of link:{jdkdocroot}/java/util/Map.html#put-K-V-[java.util.Map.put(K)]
in JCache is link:https://github.com/jsr107/jsr107spec/blob/v1.0.0-RC1/src/main/java/javax/cache/Cache.java#L230[javax.cache.Cache.getAndPut(K)].


TIP: Even though JCache API only covers standalone caching, it can be plugged
with a persistence store, and has been designed with clustering or
distribution in mind. The reason why javax.cache.Cache offers two put methods
is because standard java.util.Map put call forces implementors to calculate
the previous value. When a persistent store is in use, or the cache is
distributed, returning the previous value could be an expensive operation, and
often users call standard link:{jdkdocroot}/java/util/Map.html#put-K-V-[java.util.Map.put(K)]
without using the return value. Hence, JCache users need to think about
whether the return value is relevant to them, in which case they need to call
link:https://github.com/jsr107/jsr107spec/blob/v1.0.0-RC1/src/main/java/javax/cache/Cache.java#L230[javax.cache.Cache.getAndPut(K)] ,
otherwise they can call link:{jdkdocroot}/java/util/Map.html#put-K-V-[java.util.Map.put(K, V)]
which avoids returning the potentially expensive operation of returning the
previous value.
