CloudDatastoreCache extends AbstractRemoteCache { @Override public byte[] get(String key) { Datastore datastore = getService(); KeyFactory keyFactory = datastore.newKeyFactory().setKind(CACHE_KIND); Key datastoreKey = keyFactory.newKey(key); Entity entity = datastore.get(datastoreKey); if (entity != null) { long expiry = entity.getLong(CACHE_EXPIRY_FIELD); long now = TimeUtils.currentTimeMillis(); if (expiry == 0 || now < expiry) { return entity.getBlob(CACHE_VALUE_FIELD).toByteArray(); } } return null; }  @Override byte[] get(String key); @Override void set(String key, byte[] value); @Override void set(String key, byte[] value, int expireIn); @Override void delete(String key); int deleteExpiredItems(); int deleteExpiredItems(Integer limit); @Override Lock acquireLock(String lockName); @Override CheckResult checkConnection(); final static String CACHE_KIND; final static String CACHE_VALUE_FIELD; final static String CACHE_EXPIRY_FIELD; }
@Test public void testGet() { Datastore datastore = getService(); KeyFactory keyFactory = datastore.newKeyFactory().setKind(CACHE_KIND); Key key = keyFactory.newKey("test"); Entity.Builder builder = Entity.newBuilder(key); builder.set(CACHE_VALUE_FIELD, BlobValue.of(Blob.copyFrom("abcd".getBytes()))); builder.set(CACHE_EXPIRY_FIELD, 0); Entity entity = builder.build(); datastore.put(entity); assertArrayEquals("abcd".getBytes(), cache.get("test")); }
@Test public void testGetNotExist() { assertNull(cache.get("whatever")); }