package host
import (
"encoding/json"
"errors"
"github.com/navidrome/navidrome/plugins/pdk/go/pdk"
)
func cache_setstring(uint64) uint64
func cache_getstring(uint64) uint64
func cache_setint(uint64) uint64
func cache_getint(uint64) uint64
func cache_setfloat(uint64) uint64
func cache_getfloat(uint64) uint64
func cache_setbytes(uint64) uint64
func cache_getbytes(uint64) uint64
func cache_has(uint64) uint64
func cache_remove(uint64) uint64
type cacheSetStringRequest struct {
Key string `json:"key"`
Value string `json:"value"`
TtlSeconds int64 `json:"ttlSeconds"`
}
type cacheGetStringRequest struct {
Key string `json:"key"`
}
type cacheGetStringResponse struct {
Value string `json:"value,omitempty"`
Exists bool `json:"exists,omitempty"`
Error string `json:"error,omitempty"`
}
type cacheSetIntRequest struct {
Key string `json:"key"`
Value int64 `json:"value"`
TtlSeconds int64 `json:"ttlSeconds"`
}
type cacheGetIntRequest struct {
Key string `json:"key"`
}
type cacheGetIntResponse struct {
Value int64 `json:"value,omitempty"`
Exists bool `json:"exists,omitempty"`
Error string `json:"error,omitempty"`
}
type cacheSetFloatRequest struct {
Key string `json:"key"`
Value float64 `json:"value"`
TtlSeconds int64 `json:"ttlSeconds"`
}
type cacheGetFloatRequest struct {
Key string `json:"key"`
}
type cacheGetFloatResponse struct {
Value float64 `json:"value,omitempty"`
Exists bool `json:"exists,omitempty"`
Error string `json:"error,omitempty"`
}
type cacheSetBytesRequest struct {
Key string `json:"key"`
Value []byte `json:"value"`
TtlSeconds int64 `json:"ttlSeconds"`
}
type cacheGetBytesRequest struct {
Key string `json:"key"`
}
type cacheGetBytesResponse struct {
Value []byte `json:"value,omitempty"`
Exists bool `json:"exists,omitempty"`
Error string `json:"error,omitempty"`
}
type cacheHasRequest struct {
Key string `json:"key"`
}
type cacheHasResponse struct {
Exists bool `json:"exists,omitempty"`
Error string `json:"error,omitempty"`
}
type cacheRemoveRequest struct {
Key string `json:"key"`
}
func CacheSetString(key string, value string, ttlSeconds int64) error {
req := cacheSetStringRequest{
Key: key,
Value: value,
TtlSeconds: ttlSeconds,
}
reqBytes, err := json.Marshal(req)
if err != nil {
return err
}
reqMem := pdk.AllocateBytes(reqBytes)
defer reqMem.Free()
responsePtr := cache_setstring(reqMem.Offset())
responseMem := pdk.FindMemory(responsePtr)
responseBytes := responseMem.ReadBytes()
var response struct {
Error string `json:"error,omitempty"`
}
if err := json.Unmarshal(responseBytes, &response); err != nil {
return err
}
if response.Error != "" {
return errors.New(response.Error)
}
return nil
}
func CacheGetString(key string) (string, bool, error) {
req := cacheGetStringRequest{
Key: key,
}
reqBytes, err := json.Marshal(req)
if err != nil {
return "", false, err
}
reqMem := pdk.AllocateBytes(reqBytes)
defer reqMem.Free()
responsePtr := cache_getstring(reqMem.Offset())
responseMem := pdk.FindMemory(responsePtr)
responseBytes := responseMem.ReadBytes()
var response cacheGetStringResponse
if err := json.Unmarshal(responseBytes, &response); err != nil {
return "", false, err
}
if response.Error != "" {
return "", false, errors.New(response.Error)
}
return response.Value, response.Exists, nil
}
func CacheSetInt(key string, value int64, ttlSeconds int64) error {
req := cacheSetIntRequest{
Key: key,
Value: value,
TtlSeconds: ttlSeconds,
}
reqBytes, err := json.Marshal(req)
if err != nil {
return err
}
reqMem := pdk.AllocateBytes(reqBytes)
defer reqMem.Free()
responsePtr := cache_setint(reqMem.Offset())
responseMem := pdk.FindMemory(responsePtr)
responseBytes := responseMem.ReadBytes()
var response struct {
Error string `json:"error,omitempty"`
}
if err := json.Unmarshal(responseBytes, &response); err != nil {
return err
}
if response.Error != "" {
return errors.New(response.Error)
}
return nil
}
func CacheGetInt(key string) (int64, bool, error) {
req := cacheGetIntRequest{
Key: key,
}
reqBytes, err := json.Marshal(req)
if err != nil {
return 0, false, err
}
reqMem := pdk.AllocateBytes(reqBytes)
defer reqMem.Free()
responsePtr := cache_getint(reqMem.Offset())
responseMem := pdk.FindMemory(responsePtr)
responseBytes := responseMem.ReadBytes()
var response cacheGetIntResponse
if err := json.Unmarshal(responseBytes, &response); err != nil {
return 0, false, err
}
if response.Error != "" {
return 0, false, errors.New(response.Error)
}
return response.Value, response.Exists, nil
}
func CacheSetFloat(key string, value float64, ttlSeconds int64) error {
req := cacheSetFloatRequest{
Key: key,
Value: value,
TtlSeconds: ttlSeconds,
}
reqBytes, err := json.Marshal(req)
if err != nil {
return err
}
reqMem := pdk.AllocateBytes(reqBytes)
defer reqMem.Free()
responsePtr := cache_setfloat(reqMem.Offset())
responseMem := pdk.FindMemory(responsePtr)
responseBytes := responseMem.ReadBytes()
var response struct {
Error string `json:"error,omitempty"`
}
if err := json.Unmarshal(responseBytes, &response); err != nil {
return err
}
if response.Error != "" {
return errors.New(response.Error)
}
return nil
}
func CacheGetFloat(key string) (float64, bool, error) {
req := cacheGetFloatRequest{
Key: key,
}
reqBytes, err := json.Marshal(req)
if err != nil {
return 0, false, err
}
reqMem := pdk.AllocateBytes(reqBytes)
defer reqMem.Free()
responsePtr := cache_getfloat(reqMem.Offset())
responseMem := pdk.FindMemory(responsePtr)
responseBytes := responseMem.ReadBytes()
var response cacheGetFloatResponse
if err := json.Unmarshal(responseBytes, &response); err != nil {
return 0, false, err
}
if response.Error != "" {
return 0, false, errors.New(response.Error)
}
return response.Value, response.Exists, nil
}
func CacheSetBytes(key string, value []byte, ttlSeconds int64) error {
req := cacheSetBytesRequest{
Key: key,
Value: value,
TtlSeconds: ttlSeconds,
}
reqBytes, err := json.Marshal(req)
if err != nil {
return err
}
reqMem := pdk.AllocateBytes(reqBytes)
defer reqMem.Free()
responsePtr := cache_setbytes(reqMem.Offset())
responseMem := pdk.FindMemory(responsePtr)
responseBytes := responseMem.ReadBytes()
var response struct {
Error string `json:"error,omitempty"`
}
if err := json.Unmarshal(responseBytes, &response); err != nil {
return err
}
if response.Error != "" {
return errors.New(response.Error)
}
return nil
}
func CacheGetBytes(key string) ([]byte, bool, error) {
req := cacheGetBytesRequest{
Key: key,
}
reqBytes, err := json.Marshal(req)
if err != nil {
return nil, false, err
}
reqMem := pdk.AllocateBytes(reqBytes)
defer reqMem.Free()
responsePtr := cache_getbytes(reqMem.Offset())
responseMem := pdk.FindMemory(responsePtr)
responseBytes := responseMem.ReadBytes()
var response cacheGetBytesResponse
if err := json.Unmarshal(responseBytes, &response); err != nil {
return nil, false, err
}
if response.Error != "" {
return nil, false, errors.New(response.Error)
}
return response.Value, response.Exists, nil
}
func CacheHas(key string) (bool, error) {
req := cacheHasRequest{
Key: key,
}
reqBytes, err := json.Marshal(req)
if err != nil {
return false, err
}
reqMem := pdk.AllocateBytes(reqBytes)
defer reqMem.Free()
responsePtr := cache_has(reqMem.Offset())
responseMem := pdk.FindMemory(responsePtr)
responseBytes := responseMem.ReadBytes()
var response cacheHasResponse
if err := json.Unmarshal(responseBytes, &response); err != nil {
return false, err
}
if response.Error != "" {
return false, errors.New(response.Error)
}
return response.Exists, nil
}
func CacheRemove(key string) error {
req := cacheRemoveRequest{
Key: key,
}
reqBytes, err := json.Marshal(req)
if err != nil {
return err
}
reqMem := pdk.AllocateBytes(reqBytes)
defer reqMem.Free()
responsePtr := cache_remove(reqMem.Offset())
responseMem := pdk.FindMemory(responsePtr)
responseBytes := responseMem.ReadBytes()
var response struct {
Error string `json:"error,omitempty"`
}
if err := json.Unmarshal(responseBytes, &response); err != nil {
return err
}
if response.Error != "" {
return errors.New(response.Error)
}
return nil
}