All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.objectify.cache.KeyMemcacheService Maven / Gradle / Ivy

package com.googlecode.objectify.cache;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheService.CasValues;
import com.google.appengine.api.memcache.MemcacheService.IdentifiableValue;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

/**
 * Subset of MemcacheService used by EntityMemcache, but smart enough to translate Key into the stringified
 * version so that the memcache keys are intelligible. Also guards against calling through to the underlying
 * service when the operation is a no-op (ie, the collection of keys to operate on is empty).
 *
 * @author Jeff Schnitzer 
 */
public class KeyMemcacheService
{
	/** */
	private static final Function STRINGIFY = new Function() {
		@Override
		public String apply(Key input) {
			return KeyFactory.keyToString(input);
		}
	};

	/** */
	MemcacheService service;

	/** */
	public KeyMemcacheService(MemcacheService service) {
		this.service = service;
	}

	private  Map keyify(Map stringified) {
		Map result = Maps.newLinkedHashMap();
		for (Map.Entry entry: stringified.entrySet())
			result.put(KeyFactory.stringToKey(entry.getKey()), entry.getValue());

		return result;
	}

	private Set keyify(Set stringified) {
		Set result = Sets.newLinkedHashSet();
		for (String str: stringified)
			result.add(KeyFactory.stringToKey(str));

		return result;
	}

	private  Map stringify(Map keyified) {
		Map result = Maps.newLinkedHashMap();
		for (Map.Entry entry: keyified.entrySet())
			result.put(KeyFactory.keyToString(entry.getKey()), entry.getValue());

		return result;
	}

	private Collection stringify(Collection keys) {
		return Collections2.transform(keys, STRINGIFY);
	}

	public Map getIdentifiables(Collection keys) {
		if (keys.isEmpty())
			return Collections.emptyMap();
		
		Map map = service.getIdentifiables(stringify(keys));
		return keyify(map);
	}

	public Map getAll(Collection keys) {
		if (keys.isEmpty())
			return Collections.emptyMap();
			
		Map map = service.getAll(stringify(keys));
		return keyify(map);
	}

	public void putAll(Map map) {
		if (map.isEmpty())
			return;
		
		service.putAll(stringify(map));
	}

	public Set putIfUntouched(Map map) {
		if (map.isEmpty())
			return Collections.emptySet();
		
		Set result = service.putIfUntouched(stringify(map));
		return keyify(result);
	}

	public void deleteAll(Collection keys) {
		if (keys.isEmpty())
			return;
		
		service.deleteAll(stringify(keys));
	}

	@SuppressWarnings("deprecation")
	public void setErrorHandler(com.google.appengine.api.memcache.ErrorHandler handler) {
		service.setErrorHandler(handler);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy