Skip to content

Commit

Permalink
Remove custom LRUCache
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNuevoDia committed Dec 10, 2024
1 parent bff74aa commit c4ce17d
Showing 1 changed file with 0 additions and 61 deletions.
61 changes: 0 additions & 61 deletions dspy/utils/caching.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
from collections import OrderedDict
from hashlib import sha256
from pathlib import Path
from typing import Union
import os
import pickle

import ujson


_DEFAULT_CACHE_DIR = os.path.join(Path.home(), ".dspy_cache")
Expand All @@ -18,58 +12,3 @@ def create_subdir_in_cachedir(subdir: str) -> str:
subdir = os.path.abspath(subdir)
os.makedirs(subdir, exist_ok=True)
return subdir


class LRUCache(OrderedDict):
maxsize: int

def __init__(self, iterable, maxsize: int):
super().__init__(iterable)
self.maxsize = maxsize

@classmethod
def load(cls, file, maxsize: int) -> "LRUCache":
return cls(pickle.load(file), maxsize)

@staticmethod
def dump(obj, file) -> None:
pickle.dump([[k, v] for k, v in obj.items()], file)

def __setitem__(self, request: dict, value):
key = self.cache_key(request)

if key in self:
self.move_to_end(key)
return

if len(self) == self.maxsize:
self.popitem(last=False)

super().__setitem__(key, value)

def __getitem__(self, request: dict):
key = self.cache_key(request)
return super().__getitem__(key)

def __contains__(self, request: dict):
key = self.cache_key(request)
return super().__contains__(key)

def get(self, request: dict, default=None):
key = self.cache_key(request)
return super().get(key, default)

def __delitem__(self, request: dict):
key = self.cache_key(request)
super().__delitem__(key)

def pop(self, request: dict, default=None):
key = self.cache_key(request)
return super().pop(key, default)

@staticmethod
def cache_key(request: Union[dict, str]) -> str:
params = request
if isinstance(request, dict):
params = {k: v for k, v in request.items() if not callable(v)}
return sha256(ujson.dumps(params, sort_keys=True).encode()).hexdigest()

0 comments on commit c4ce17d

Please sign in to comment.