Add format query parameter

This commit is contained in:
Jacob Gunther
2023-08-27 21:38:35 -05:00
parent 4adcd85f4b
commit d11a1c58c5
9 changed files with 204 additions and 169 deletions

View File

@@ -1,9 +1,10 @@
package main
import (
"encoding/json"
"fmt"
"image"
"net/url"
"strconv"
)
type ResultCacheKey struct {
@@ -14,21 +15,15 @@ type ResultCacheKey struct {
}
// GetCacheKey returns the key used in the cache based on the rendering options, calculated as an SHA-256 hash.
func GetResultCacheKey(uuid, renderType string, opts *QueryParams) (string, error) {
rawKey := ResultCacheKey{
UUID: uuid,
Type: renderType,
Scale: opts.Scale,
Overlay: opts.Overlay,
}
func GetResultCacheKey(uuid, renderType string, opts *QueryParams) string {
values := &url.Values{}
values.Set("uuid", uuid)
values.Set("type", renderType)
values.Set("scale", strconv.FormatInt(int64(opts.Scale), 10))
values.Set("overlay", strconv.FormatBool(opts.Overlay))
values.Set("format", opts.Format)
rawKeyData, err := json.Marshal(rawKey)
if err != nil {
return "", err
}
return fmt.Sprintf("result:%s", SHA256(rawKeyData)), nil
return SHA256(values.Encode())
}
// GetCachedRenderResult returns the render result from Redis cache, or nil if it does not exist or cache is disabled.
@@ -37,13 +32,7 @@ func GetCachedRenderResult(renderType, uuid string, opts *QueryParams) ([]byte,
return nil, nil
}
key, err := GetResultCacheKey(uuid, renderType, opts)
if err != nil {
return nil, err
}
return r.GetBytes(key)
return r.GetBytes(fmt.Sprintf("result:%s", GetResultCacheKey(uuid, renderType, opts)))
}
// SetCachedRenderResult puts the render result into cache, or does nothing is cache is disabled.
@@ -52,13 +41,7 @@ func SetCachedRenderResult(renderType, uuid string, opts *QueryParams, data []by
return nil
}
key, err := GetResultCacheKey(uuid, renderType, opts)
if err != nil {
return err
}
return r.Set(key, data, *config.Cache.RenderCacheDuration)
return r.Set(fmt.Sprintf("result:%s", GetResultCacheKey(uuid, renderType, opts)), data, *config.Cache.RenderCacheDuration)
}
// GetCachedSkin returns the raw skin of a player by UUID from the cache, also returning if the player has a slim player model.