use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest.SetPolicy in project appengine-java-standard by GoogleCloudPlatform.
the class LocalMemcacheService method set.
public MemcacheSetResponse set(Status status, MemcacheSetRequest req) {
MemcacheSetResponse.Builder result = MemcacheSetResponse.newBuilder();
final String namespace = req.getNameSpace();
for (int i = 0; i < req.getItemCount(); i++) {
MemcacheSetRequest.Item item = req.getItem(i);
Key key = new Key(item.getKey().toByteArray());
SetPolicy policy = item.getSetPolicy();
Map<Key, Long> timeoutMap = getOrMakeSubMap(deleteHold, namespace);
Long timeout = timeoutMap.get(key);
if (timeout != null && policy == SetPolicy.SET) {
// A SET operation overrides and clears any timeout that may exist
timeout = null;
timeoutMap.remove(key);
}
if ((timeout != null && clock.getCurrentTime() < timeout) || (policy == SetPolicy.CAS && !item.hasCasId())) {
result.addSetStatus(SetStatusCode.NOT_STORED);
continue;
}
synchronized (mockCache) {
CacheEntry existingEntry = getWithExpiration(namespace, key);
if ((policy == SetPolicy.REPLACE && existingEntry == null) || (policy == SetPolicy.ADD && existingEntry != null) || (policy == SetPolicy.CAS && existingEntry == null)) {
result.addSetStatus(SetStatusCode.NOT_STORED);
} else if (policy == SetPolicy.CAS && (!existingEntry.hasCasId() || existingEntry.getCasId() != item.getCasId())) {
result.addSetStatus(SetStatusCode.EXISTS);
} else {
long expiry = item.hasExpirationTime() ? (long) item.getExpirationTime() : 0L;
byte[] value = item.getValue().toByteArray();
int flags = item.getFlags();
// We create a new cacheEntry every time (rather than updating existing ones) to
// avoid having to synchronize on reads. (Otherwise a reader on another thread
// could be exposed to a partially modified entry).
CacheEntry newEntry = new CacheEntry(namespace, key, value, flags, expiry * 1000);
internalSet(namespace, key, newEntry);
result.addSetStatus(SetStatusCode.STORED);
}
}
}
status.setSuccessful(true);
return result.build();
}
Aggregations