Index: third_party/google-endpoints/cachetools/keys.py |
diff --git a/third_party/google-endpoints/cachetools/keys.py b/third_party/google-endpoints/cachetools/keys.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..887fb302fdd5d6861ade3ef8467b72157bced82d |
--- /dev/null |
+++ b/third_party/google-endpoints/cachetools/keys.py |
@@ -0,0 +1,38 @@ |
+__all__ = ('hashkey', 'typedkey') |
+ |
+ |
+class _HashedTuple(tuple): |
+ |
+ __hashvalue = None |
+ |
+ def __hash__(self, hash=tuple.__hash__): |
+ hashvalue = self.__hashvalue |
+ if hashvalue is None: |
+ self.__hashvalue = hashvalue = hash(self) |
+ return hashvalue |
+ |
+ def __add__(self, other, add=tuple.__add__): |
+ return _HashedTuple(add(self, other)) |
+ |
+ def __radd__(self, other, add=tuple.__add__): |
+ return _HashedTuple(add(other, self)) |
+ |
+_kwmark = (object(),) |
+ |
+ |
+def hashkey(*args, **kwargs): |
+ """Return a cache key for the specified hashable arguments.""" |
+ |
+ if kwargs: |
+ return _HashedTuple(args + sum(sorted(kwargs.items()), _kwmark)) |
+ else: |
+ return _HashedTuple(args) |
+ |
+ |
+def typedkey(*args, **kwargs): |
+ """Return a typed cache key for the specified hashable arguments.""" |
+ |
+ key = hashkey(*args, **kwargs) |
+ key += tuple(type(v) for v in args) |
+ key += tuple(type(v) for _, v in sorted(kwargs.items())) |
+ return key |