MCPcopy Create free account
hub / github.com/BerriAI/litellm / set_cache

Method set_cache

litellm/caching/s3_cache.py:63–100  ·  view source on GitHub ↗
(self, key, value, **kwargs)

Source from the content-addressed store, hash-verified

61 return self.key_prefix + key.replace(":", "/")
62
63 def set_cache(self, key, value, **kwargs):
64 try:
65 print_verbose(f"LiteLLM SET Cache - S3. Key={key}. Value={value}")
66 ttl = kwargs.get("ttl", None)
67 # Convert value to JSON before storing in S3
68 serialized_value = json.dumps(value)
69 key = self._to_s3_key(key)
70
71 if ttl is not None:
72 cache_control = f"immutable, max-age={ttl}, s-maxage={ttl}"
73
74 # Calculate expiration time
75 expiration_time = datetime.now(timezone.utc) + timedelta(seconds=ttl)
76 # Upload the data to S3 with the calculated expiration time
77 self.s3_client.put_object(
78 Bucket=self.bucket_name,
79 Key=key,
80 Body=serialized_value,
81 Expires=expiration_time,
82 CacheControl=cache_control,
83 ContentType="application/json",
84 ContentLanguage="en",
85 ContentDisposition=f'inline; filename="{key}.json"',
86 )
87 else:
88 cache_control = "immutable, max-age=31536000, s-maxage=31536000"
89 # Upload the data to S3 without specifying Expires
90 self.s3_client.put_object(
91 Bucket=self.bucket_name,
92 Key=key,
93 Body=serialized_value,
94 CacheControl=cache_control,
95 ContentType="application/json",
96 ContentLanguage="en",
97 ContentDisposition=f'inline; filename="{key}.json"',
98 )
99 except Exception as e:
100 print_verbose(f"S3 Caching: set_cache() - Got exception from S3: {e}")
101
102 async def async_set_cache(self, key, value, **kwargs):
103 """

Calls 5

_to_s3_keyMethod · 0.95
print_verboseFunction · 0.90
put_objectMethod · 0.80
getMethod · 0.65
nowMethod · 0.45

Tested by 2

test_s3_cache_set_cacheFunction · 0.76