internetarchive / internetarchive/openlibrary

Refactor openlibrary.core.cache get function

Open
#4,422 1 comment 0 reactions 0 assignees View on GitHub
Lead: @scottbarnes Priority: 3
Dominant language
Python
Stars
6.7k
Forks
2k
Avg merge
2d 19h
Merged PRs (30d)
138

Description

In https://github.com/internetarchive/openlibrary/pull/4296#discussion_r557523924
The `class MemcacheCache(Cache)` `get` function: https://github.com/internetarchive/openlibrary/blob/master/openlibrary/core/cache.py#L312-L317

```python
def get(self, key):
key = web.safestr(key)
stats.begin("memcache.get", key=key)
value = self.memcache.get(key)
stats.end(hit=value is not None)
return value and simplejson.loads(value)
```

@cclauss says:

> I do not love this approach for a return statement because we (and the caller) are clueless about what the return type is going to be... If value is '' then we return a str, if If value is 0 then we return an int, If value is 0.0 then we return a float, If value is {} then we return a dict, etc.
>
> What about return value and json.loads(value) or {} (we always return a dict) or return value and json.loads(value) or None (we return an Optional[dict])?

I suggested opening this issue instead of solving this refactor in that pull request because the memcache get function is used in quite a few places and changing the return value needs to be well tested.

#### Stakeholders
@cclauss @dherbst

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.