private VersionedKey loadKeyFromCouchbase() { Optional<String> optionalKey = connection.get(key); return optionalKey.flatMap(val -> Optional.of(JacksonUtil.convertFromJson(val, VersionedKey.class))) .orElse(newKey(0L)); }
@GET @Path("/put") public String put() { client.put("hello", "world"); return "added"; } }
private void delete(String withVersion) { connection.delete(withVersion); }
@SuppressWarnings("unchecked") private Object nonAtomicload(String newVersionedKey) throws Throwable { Data data = (Data) connection.get(newVersionedKey) .orElseThrow(() -> { return new ManifestComparatorKeyNotFoundException( "Missing versioned key " + newVersionedKey + " - likely data changed during read"); }); logger.info("Loaded new data with date {} for key {}, versionedKey {}, versionedKey from data ", new Object[] { data.getDate(), key, newVersionedKey, data.getVersionedKey() }); return data.getData(); }
@GET @Path("/put") public String put() { client.put("hello", "world"); return "added"; } }
@GET @Path("/get") public String bucket() { return client.get("hello") .toString(); }
@GET @Path("/put") public String put() { client.put("hello", "world"); return "added"; } }
@GET @Path("/get") public String bucket() { return client.get("hello") .toString(); }
@GET @Path("/put") public String put() { client.put("hello", "world"); return "added"; } }
@GET @Path("/get") public String bucket() { return client.get("hello").toString(); }
@GET @Path("/put") public String put() { client.put("hello", "world"); return "added"; } }
@GET @Path("/get") public String bucket() { return client.get("hello").toString(); }
/** * Save provided data with the key this ManifestComparator manages bump the * versioned key version. * * NB : To avoid race conditions - make sure only one service (an elected * leader) can write at a time (see micro-mysql for a mysql distributed * lock, or micro-curator for a curator / zookeeper distributed lock * implementation). * * @param data * to save */ @Override public void saveAndIncrement(T data) { Either<Void, T> oldData = this.data; VersionedKey newVersionedKey = increment(); logger.info("Saving data with key {}, new version is {}", key, newVersionedKey.toJson()); connection.put(newVersionedKey.toJson(), new Data( data, new Date(), newVersionedKey.toJson())); connection.put(key, newVersionedKey.toJson()); try { this.data = Either.right(data); delete(versionedKey); } catch (Throwable t) { this.data = oldData; } finally { versionedKey = newVersionedKey.toJson(); } }
@GET @Path("/get") public String bucket() { return client.get("hello") .toString(); }
/** * Load data from remote store if stale */ @Override public synchronized boolean load() { Either<Void, T> oldData = data; String oldKey = versionedKey; try { if (isOutOfDate()) { String newVersionedKey = (String) connection.get(key) .get(); data = Either.right((T) nonAtomicload(newVersionedKey)); versionedKey = newVersionedKey; } else { return false; } } catch (Throwable e) { data = oldData; versionedKey = oldKey; logger.debug(e.getMessage(), e); throw ExceptionSoftener.throwSoftenedException(e); } return true; }
@Test public void createDistributedCacheMemcachedOff() throws IOException, URISyntaxException { config.setCouchbaseClientEnabled(false); DistributedMap<Object> cache = config.simpleCouchbaseClient(); assertThat(cache.get("hello"), is(Optional.empty())); }