summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-storage/s3/cache.go
diff options
context:
space:
mode:
authorLibravatar kim <grufwub@gmail.com>2025-11-10 12:36:59 +0100
committerLibravatar tobi <tobi.smethurst@protonmail.com>2025-11-17 14:13:24 +0100
commitac877bde815827f7aa1eeb3a6f0513d4c7503ad0 (patch)
tree7a315d820a5be3232dd5bfc96857017c38e906fd /vendor/codeberg.org/gruf/go-storage/s3/cache.go
parent[chore] update dependencies (#4542) (diff)
downloadgotosocial-ac877bde815827f7aa1eeb3a6f0513d4c7503ad0.tar.xz
[performance] add optional S3 object info caching (#4546)
This adds an optional S3 object info cache to the S3 storage driver backend (see [here](https://codeberg.org/gruf/go-storage/releases/tag/v0.4.0)) to reduce S3 calls largely during media cleanup operations, but it should also help in other situations cutting back on S3 calls when for example a key is already known to not exist. Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4546 Co-authored-by: kim <grufwub@gmail.com> Co-committed-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/codeberg.org/gruf/go-storage/s3/cache.go')
-rw-r--r--vendor/codeberg.org/gruf/go-storage/s3/cache.go94
1 files changed, 94 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-storage/s3/cache.go b/vendor/codeberg.org/gruf/go-storage/s3/cache.go
new file mode 100644
index 000000000..37ff9f321
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-storage/s3/cache.go
@@ -0,0 +1,94 @@
+package s3
+
+import (
+ "time"
+
+ "github.com/minio/minio-go/v7"
+)
+
+// EntryCache should provide a cache of
+// S3 object information for speeding up
+// Get(), Stat() and Remove() operations.
+type EntryCache interface {
+
+ // Get should return 'found' = true when information is cached,
+ // with 'info' optionally being nilable to allow caching errors.
+ Get(key string) (info *CachedObjectInfo, found bool)
+
+ // Put should cache the given information under key, with
+ // nil CachedObjectInfo{} meaning a 'not found' response.
+ Put(key string, info *CachedObjectInfo)
+}
+
+// CachedObjectInfo provides the minimum cacheable
+// set of S3 object information that may be returned
+// from a Get() or Stat() operation, or on Put().
+type CachedObjectInfo struct {
+ Key string
+ ETag string
+ Size int64
+ ContentType string
+ LastModified time.Time
+ VersionID string
+}
+
+// ToObjectInfo converts CachedObjectInfo to returnable minio.ObjectInfo.
+func (info *CachedObjectInfo) ToObjectInfo() minio.ObjectInfo {
+ return minio.ObjectInfo{
+ Key: info.Key,
+ ETag: info.ETag,
+ Size: info.Size,
+ ContentType: info.ContentType,
+ LastModified: info.LastModified,
+ VersionID: info.VersionID,
+ }
+}
+
+// cacheGet wraps cache.Get() operations to check if cache is nil.
+func cacheGet(cache EntryCache, key string) (*CachedObjectInfo, bool) {
+ if cache != nil {
+ return cache.Get(key)
+ }
+ return nil, false
+}
+
+// objectToCachedObjectInfo converts minio.ObjectInfo to CachedObjectInfo for caching.
+func objectToCachedObjectInfo(info minio.ObjectInfo) *CachedObjectInfo {
+ return &CachedObjectInfo{
+ Key: info.Key,
+ ETag: info.ETag,
+ Size: info.Size,
+ ContentType: info.ContentType,
+ LastModified: info.LastModified,
+ VersionID: info.VersionID,
+ }
+}
+
+// cacheObject wraps cache.Put() operations to check if cache is nil.
+func cacheObject(cache EntryCache, key string, info minio.ObjectInfo) {
+ if cache != nil {
+ cache.Put(key, objectToCachedObjectInfo(info))
+ }
+}
+
+// cacheUpload wraps cache.Put() operations to check if cache is nil, uses ContentType from given opts.
+func cacheUpload(cache EntryCache, key string, info minio.UploadInfo, opts minio.PutObjectOptions) {
+ if cache != nil {
+ cache.Put(key, &CachedObjectInfo{
+ Key: info.Key,
+ ETag: info.ETag,
+ Size: info.Size,
+ ContentType: opts.ContentType,
+ LastModified: info.LastModified,
+ VersionID: info.VersionID,
+ })
+ }
+}
+
+// cacheNotFound wraps cache.Put() to check if cache is
+// nil, storing a nil entry (i.e. not found) in cache.
+func cacheNotFound(cache EntryCache, key string) {
+ if cache != nil {
+ cache.Put(key, nil)
+ }
+}