Skip to content

Commit

Permalink
cache - revert the haskey() customization
Browse files Browse the repository at this point in the history
  • Loading branch information
kentnsw committed Aug 26, 2022
1 parent f687c58 commit 41d03be
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 16 deletions.
16 changes: 6 additions & 10 deletions c7n/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ def load(self):
def get(self, key):
pass

def haskey(self, key):
pass

def save(self, key, data):
pass

Expand Down Expand Up @@ -85,9 +82,6 @@ def load(self):
def get(self, key):
return self.data.get(encode(key))

def haskey(self, key):
return pickle.dumps(key) in self.data # nosemgrep

def save(self, key, data):
self.data[encode(key)] = data

Expand Down Expand Up @@ -121,10 +115,7 @@ def __init__(self, config):
self.cache_path = resolve_path(config.cache)
self.conn = None

def haskey(self, key):
return pickle.dumps(key) in self.data # nosemgrep

def load(self):
def init(self):
# migration from pickle cache file
if os.path.exists(self.cache_path):
with open(self.cache_path, 'rb') as fh:
Expand All @@ -142,9 +133,14 @@ def load(self):
cursor.execute(
'delete from c7n_cache where create_date < ?',
[datetime.utcnow() - timedelta(minutes=self.cache_period)])

def load(self):
if not self.conn:
self.init()
return True

def get(self, key):
self.load()
with self.conn as cursor:
r = cursor.execute(
'select value, create_date from c7n_cache where key = ?',
Expand Down
4 changes: 1 addition & 3 deletions c7n/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,7 @@ def get_values(self):
# the results.
key = [self.data.get(i) for i in ('url', 'format', 'expr')]
contents = self.cache.get(("value-from", key))
# NOTE no matter the contents is None or not, return it
# if contents is not None:
if self.cache.haskey(("value-from", key)):
if contents is not None:
return contents

contents = self._get_values()
Expand Down
3 changes: 0 additions & 3 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ def save(self, key, data):
self.saves += 1
self.state[pickle.dumps(key)] = data

def haskey(self, key):
return pickle.dumps(key) in self.state


class FakeResolver:

Expand Down

0 comments on commit 41d03be

Please sign in to comment.