forked from phoenix/litellm-mirror
(caching) improve s3 backend by specifying cache-control and content-type
This commit is contained in:
parent
b103ab1f0b
commit
c54e0813b4
1 changed files with 9 additions and 1 deletions
|
@ -155,6 +155,7 @@ class S3Cache(BaseCache):
|
||||||
# Convert value to JSON before storing in S3
|
# Convert value to JSON before storing in S3
|
||||||
serialized_value = str(value)
|
serialized_value = str(value)
|
||||||
if ttl is not None:
|
if ttl is not None:
|
||||||
|
cache_control = f"immutable, max-age={ttl}, s-maxage={ttl}"
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
# Calculate expiration time
|
# Calculate expiration time
|
||||||
|
@ -166,11 +167,18 @@ class S3Cache(BaseCache):
|
||||||
Key=key,
|
Key=key,
|
||||||
Body=serialized_value,
|
Body=serialized_value,
|
||||||
Expires=expiration_time,
|
Expires=expiration_time,
|
||||||
|
CacheControl=cache_control,
|
||||||
|
ContentType="application/json"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
cache_control = "immutable, max-age=31536000, s-maxage=31536000"
|
||||||
# Upload the data to S3 without specifying Expires
|
# Upload the data to S3 without specifying Expires
|
||||||
self.s3_client.put_object(
|
self.s3_client.put_object(
|
||||||
Bucket=self.bucket_name, Key=key, Body=serialized_value
|
Bucket=self.bucket_name,
|
||||||
|
Key=key,
|
||||||
|
Body=serialized_value,
|
||||||
|
CacheControl=cache_control,
|
||||||
|
ContentType="application/json"
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# NON blocking - notify users S3 is throwing an exception
|
# NON blocking - notify users S3 is throwing an exception
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue