wheezy.caching / src / wheezy / caching / patterns.py

Diff from to

src/wheezy/caching/patterns.py

 from time import time
 
 
+def get_or_add(key, create_factory, dependency_factory=None,
+               time=0, namespace=None, cache=None):
+    """ Cache Pattern: get an item by *key* from *cache* and
+        if it is not available use *create_factory* to aquire one.
+        If result is not `None` use cache `add` operation to store
+        result and if operation succeed use *dependency_factory*
+        to get an instance of `CacheDependency` to add *key* to it.
+    """
+    result = cache.get(key, namespace)
+    if result is not None:
+        return result
+    result = create_factory()
+    if result is not None:
+        succeed = cache.add(key, result, time, namespace)
+        if succeed and dependency_factory is not None:
+            dependency = dependency_factory()
+            dependency.add(key, namespace)
+    return result
+
+
+def get_or_set(key, create_factory, dependency_factory=None,
+               time=0, namespace=None, cache=None):
+    """ Cache Pattern: get an item by *key* from *cache* and
+        if it is not available use *create_factory* to aquire one.
+        If result is not `None` use cache `set` operation to store
+        result and use *dependency_factory* to get an instance of
+        `CacheDependency` to add *key* to it.
+    """
+    result = cache.get(key, namespace)
+    if result is not None:
+        return result
+    result = create_factory()
+    if result is not None:
+        cache.set(key, result, time, namespace)
+        if dependency_factory is not None:
+            dependency = dependency_factory()
+            dependency.add(key, namespace)
+    return result
+
+
 class OnePass(object):
     """ A solution to `Thundering Head` problem.
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.