ATLAS Offline Software
AtlRunQueryCache.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 
5 from __future__ import print_function
6 import pickle
7 import os
8 import sys
9 
10 DEBUG=False
11 
12 class Cache(object):
13  def __init__(self,cachedir,form):
14  self._cache = {}
15  self.cachedir = cachedir.rstrip('/') if os.path.exists(cachedir) else None
16  self.form = form
17  if self.cachedir:
18  print ("Initializing the cache at",cachedir.rstrip('/'))
19 
20 
21  def __call__(self, f, *args, **kwds):
22  if not self.cachedir:
23  return f # caching is automatically disabled if no cache directory exists
24 
25  print ("Providing cache functionality for function '%s()'" % f.func_name)
26  def newf(*args, **kwds):
27  key = kwds['cachekey']
28  if not self.__is_cached(key):
29  self.__write_to_cache(key,f(*args, **kwds))
30  else:
31  if DEBUG:
32  print ("DEBUG: returning cached value for '%s'" % (self.form % key))
33  pass
34  return self._cache[key]
35  return newf
36 
37  def __name_pickle_cache(self,key):
38  filename = self.form % key
39  return '%s/%s.pickle' % (self.cachedir, filename)
40 
41 
42  def __write_to_cache(self,key, value):
43  # put into transient cache
44  self._cache[key] = value
45 
46  # store pickled version
47  pfname = self.__name_pickle_cache(key)
48  pf = open( pfname, 'w' )
49  try:
50  pickle.dump(value, pf)
51  except pickle.PicklingError as err:
52  print ('ERROR: could not write to cache: %s (%s)' % (pfname, err))
53  sys.exit(1)
54  pf.close()
55 
56  def __is_cached(self,key):
57  # check transient cache
58  if key in self._cache:
59  return True
60 
61  # check for pickled version
62  pfname = self.__name_pickle_cache(key)
63  try:
64  pf = open( pfname, 'r' )
65  except OSError:
66  if DEBUG:
67  print ('DEBUG: could not read from cache: ' + pfname)
68  return False
69  try:
70  self._cache[key] = pickle.load(pf)
71  except pickle.UnpicklingError as err:
72  print ("ERROR: could not unpickle %s (%s)" % (pfname, err))
73  sys.exit(1)
74  pf.close()
75  return True
76 
77 
78 
79 
80 
81 if __name__ == "__main__":
82  @Cache("/afs/cern.ch/user/s/stelzer/runsummary/cache/","simple_%i_second%i")
83  def aFunction(x,y,cachekey):
84  print ("expensive")
85  return (3*x,y*y,x+y)
86 
87  x=13
88  print (aFunction(x,y=2,cachekey=(x,2)))
89  print (aFunction(x,1,cachekey=(x,1)))
90 
python.CaloRecoConfig.f
f
Definition: CaloRecoConfig.py:127
python.utils.AtlRunQueryCache.Cache.__write_to_cache
def __write_to_cache(self, key, value)
Definition: AtlRunQueryCache.py:42
python.utils.AtlRunQueryCache.Cache.cachedir
cachedir
Definition: AtlRunQueryCache.py:15
python.utils.AtlRunQueryCache.Cache
Definition: AtlRunQueryCache.py:12
python.utils.AtlRunQueryCache.Cache.__call__
def __call__(self, f, *args, **kwds)
Definition: AtlRunQueryCache.py:21
python.utils.AtlRunQueryCache.aFunction
def aFunction(x, y, cachekey)
Definition: AtlRunQueryCache.py:83
python.utils.AtlRunQueryCache.Cache.__is_cached
def __is_cached(self, key)
Definition: AtlRunQueryCache.py:56
python.utils.AtlRunQueryCache.Cache.form
form
Definition: AtlRunQueryCache.py:16
python.utils.AtlRunQueryCache.Cache._cache
_cache
Definition: AtlRunQueryCache.py:14
Trk::open
@ open
Definition: BinningType.h:40
pickleTool.object
object
Definition: pickleTool.py:30
python.utils.AtlRunQueryCache.Cache.__name_pickle_cache
def __name_pickle_cache(self, key)
Definition: AtlRunQueryCache.py:37
python.utils.AtlRunQueryCache.Cache.__init__
def __init__(self, cachedir, form)
Definition: AtlRunQueryCache.py:13