Skip to content

Commit

Permalink
Copy prebuilt default flags
Browse files Browse the repository at this point in the history
  • Loading branch information
bisho committed Nov 22, 2023
1 parent cede4b3 commit 33992eb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
68 changes: 40 additions & 28 deletions src/meta_memcache/commands/high_level_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@
T = TypeVar("T")
_REFILL_FAILURE_HANDLING = FailureHandling(track_write_failures=False)

DEFAULT_FLAGS: Set[Flag] = {
Flag.RETURN_VALUE,
Flag.RETURN_TTL,
Flag.RETURN_CLIENT_FLAG,
Flag.RETURN_LAST_ACCESS,
Flag.RETURN_FETCHED,
}
DEFAULT_CAS_FLAGS: Set[Flag] = {
Flag.RETURN_VALUE,
Flag.RETURN_TTL,
Flag.RETURN_CLIENT_FLAG,
Flag.RETURN_LAST_ACCESS,
Flag.RETURN_FETCHED,
Flag.RETURN_CAS_TOKEN,
}


class HighLevelCommandMixinWithMetaCommands(
HighLevelCommandsProtocol, MetaCommandsProtocol, Protocol
Expand Down Expand Up @@ -330,20 +346,18 @@ def _multi_get(
recache_policy: Optional[RecachePolicy] = None,
return_cas_token: bool = False,
) -> Dict[Key, Optional[Value]]:
flags = {
Flag.RETURN_VALUE,
Flag.RETURN_TTL,
Flag.RETURN_CLIENT_FLAG,
Flag.RETURN_LAST_ACCESS,
Flag.RETURN_FETCHED,
}
if return_cas_token:
flags.add(Flag.RETURN_CAS_TOKEN)
int_flags = {}
if recache_policy:
int_flags[IntFlag.RECACHE_TTL] = recache_policy.ttl
if touch_ttl is not None and touch_ttl >= 0:
int_flags[IntFlag.CACHE_TTL] = touch_ttl
flags = DEFAULT_CAS_FLAGS.copy()
else:
flags = DEFAULT_FLAGS.copy()
if recache_policy is None and touch_ttl is None:
int_flags = None
else:
int_flags = {}
if recache_policy:
int_flags[IntFlag.RECACHE_TTL] = recache_policy.ttl
if touch_ttl is not None and touch_ttl >= 0:
int_flags[IntFlag.CACHE_TTL] = touch_ttl

results = self.meta_multiget(
keys=[key if isinstance(key, Key) else Key(key) for key in keys],
Expand Down Expand Up @@ -379,22 +393,20 @@ def _get(
return_cas_token: bool = False,
) -> Optional[Value]:
key = key if isinstance(key, Key) else Key(key)
flags = {
Flag.RETURN_VALUE,
Flag.RETURN_TTL,
Flag.RETURN_CLIENT_FLAG,
Flag.RETURN_LAST_ACCESS,
Flag.RETURN_FETCHED,
}
if return_cas_token:
flags.add(Flag.RETURN_CAS_TOKEN)
int_flags = {}
if lease_policy:
int_flags[IntFlag.MISS_LEASE_TTL] = lease_policy.ttl
if recache_policy:
int_flags[IntFlag.RECACHE_TTL] = recache_policy.ttl
if touch_ttl is not None and touch_ttl >= 0:
int_flags[IntFlag.CACHE_TTL] = touch_ttl
flags = DEFAULT_CAS_FLAGS.copy()
else:
flags = DEFAULT_FLAGS.copy()
if lease_policy is None and recache_policy is None and touch_ttl is None:
int_flags = None
else:
int_flags = {}
if lease_policy:
int_flags[IntFlag.MISS_LEASE_TTL] = lease_policy.ttl
if recache_policy:
int_flags[IntFlag.RECACHE_TTL] = recache_policy.ttl
if touch_ttl is not None and touch_ttl >= 0:
int_flags[IntFlag.CACHE_TTL] = touch_ttl

result = self.meta_get(key, flags=flags, int_flags=int_flags)
return self._process_get_result(key, result)
Expand Down
2 changes: 1 addition & 1 deletion tests/probabilistic_hot_cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def random(monkeypatch) -> Mock:
Flag.RETURN_FETCHED,
Flag.RETURN_CLIENT_FLAG,
},
"int_flags": {},
"int_flags": None,
"token_flags": None,
"failure_handling": DEFAULT_FAILURE_HANDLING,
}
Expand Down

0 comments on commit 33992eb

Please sign in to comment.