github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/services/caches/cache.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package caches
    19  
    20  import (
    21  	"github.com/aacfactory/errors"
    22  	"github.com/aacfactory/fns/context"
    23  	"github.com/aacfactory/fns/runtime"
    24  	"github.com/aacfactory/fns/services"
    25  	"github.com/aacfactory/logs"
    26  	"time"
    27  )
    28  
    29  type KeyParam interface {
    30  	CacheKey(ctx context.Context) (key []byte, err error)
    31  }
    32  
    33  type Store interface {
    34  	services.Component
    35  	Get(ctx context.Context, key []byte) (value []byte, has bool, err error)
    36  	Set(ctx context.Context, key []byte, value []byte, ttl time.Duration) (err error)
    37  	Remove(ctx context.Context, key []byte) (err error)
    38  }
    39  
    40  type defaultStore struct {
    41  	log    logs.Logger
    42  	prefix []byte
    43  }
    44  
    45  func (store *defaultStore) Name() (name string) {
    46  	return "default"
    47  }
    48  
    49  func (store *defaultStore) Construct(options services.Options) (err error) {
    50  	store.log = options.Log
    51  	store.prefix = []byte("fns:caches:")
    52  	return
    53  }
    54  
    55  func (store *defaultStore) Shutdown(_ context.Context) {
    56  	return
    57  }
    58  
    59  func (store *defaultStore) Get(ctx context.Context, key []byte) (value []byte, has bool, err error) {
    60  	st := runtime.SharedStore(ctx)
    61  	value, has, err = st.Get(ctx, append(store.prefix, key...))
    62  	if err != nil {
    63  		err = errors.Warning("fns: get cache failed").WithMeta("key", string(key)).WithCause(err)
    64  		return
    65  	}
    66  	return
    67  }
    68  
    69  func (store *defaultStore) Set(ctx context.Context, key []byte, value []byte, ttl time.Duration) (err error) {
    70  	st := runtime.SharedStore(ctx)
    71  	err = st.SetWithTTL(ctx, append(store.prefix, key...), value, ttl)
    72  	if err != nil {
    73  		err = errors.Warning("fns: set cache failed").WithMeta("key", string(key)).WithCause(err)
    74  		return
    75  	}
    76  	return
    77  }
    78  
    79  func (store *defaultStore) Remove(ctx context.Context, key []byte) (err error) {
    80  	st := runtime.SharedStore(ctx)
    81  	err = st.Remove(ctx, append(store.prefix, key...))
    82  	if err != nil {
    83  		err = errors.Warning("fns: remove cache failed").WithMeta("key", string(key)).WithCause(err)
    84  		return
    85  	}
    86  	return
    87  }