refactor: replace cachetools with stdlib OrderedDict-based LRUCache#36
refactor: replace cachetools with stdlib OrderedDict-based LRUCache#36isaacbmiller wants to merge 1 commit intomainfrom
Conversation
Greptile SummaryThis PR removes the
Confidence Score: 5/5Safe to merge — the custom LRUCache correctly replicates the cachetools contract used by DSPy, cloudpickle serialization is preserved via reduce, and thread safety is unaffected. The replacement implementation faithfully covers every code path the Cache class exercises: LRU promotion on read and write, front-of-dict eviction when capacity is exceeded, and pickle round-tripping. The existing 21-test suite covers memory cache get/put, save/load, and eviction. No behavioral regression is apparent. No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["LRUCache.__setitem__(key, value)"] --> B{key in cache?}
B -- Yes --> C["move_to_end(key)\n(promote to MRU)"]
C --> D["super().__setitem__(key, value)\n(update in-place)"]
B -- No --> D
D --> E{len > maxsize?}
E -- Yes --> F["pop(next(iter(self)))\n(evict LRU item)"]
F --> E
E -- No --> G[Done]
H["LRUCache.__getitem__(key)"] --> I["move_to_end(key)\n(promote to MRU)"]
I --> J["return super().__getitem__(key)"]
K["LRUCache.__reduce__()"] --> L["return (LRUCache, (maxsize,), None, None, iter(items))"]
L --> M["Pickle reconstructs:\nLRUCache(maxsize)\nthen obj[k]=v for each item"]
Reviews (3): Last reviewed commit: "refactor: replace cachetools with stdlib..." | Re-trigger Greptile |
9eb2f08 to
99d30c7
Compare
Replace cachetools.LRUCache with a ~30-line OrderedDict subclass that covers the subset used by DSPy: maxsize, get/set/contains/clear, and cloudpickle serialization via __reduce__. Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
99d30c7 to
5208ae1
Compare
Summary
Remove
cachetoolsas a direct dependency by replacingcachetools.LRUCachewith a ~30-lineOrderedDictsubclass.Changes
dspy/clients/cache.py: AddedLRUCache(OrderedDict)class coveringmaxsize,get/set/contains/clear, LRU eviction viamove_to_end, and cloudpickle serialization via__reduce__.tests/clients/test_cache.py: Updated import fromcachetoolstodspy.clients.cache.pyproject.toml: Removedcachetools>=5.5.0from dependencies.Notes
self._lock = threading.RLock()in theCacheclass, not inLRUCacheitself.