github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/services/caches/set.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  	"fmt"
    22  	"github.com/aacfactory/avro"
    23  	"github.com/aacfactory/errors"
    24  	"github.com/aacfactory/fns/commons/bytex"
    25  	"github.com/aacfactory/fns/context"
    26  	"github.com/aacfactory/fns/runtime"
    27  	"github.com/aacfactory/fns/services"
    28  	"time"
    29  )
    30  
    31  func Set(ctx context.Context, param interface{}, value interface{}, ttl time.Duration) (err error) {
    32  	if param == nil {
    33  		err = errors.Warning("fns: set cache failed").WithCause(fmt.Errorf("param is nil"))
    34  		return
    35  	}
    36  	if value == nil {
    37  		err = errors.Warning("fns: set cache failed").WithCause(fmt.Errorf("value is invalid"))
    38  		return
    39  	}
    40  	kp, ok := param.(KeyParam)
    41  	if !ok {
    42  		err = errors.Warning("fns: set cache failed").WithCause(fmt.Errorf("param dose not implement caches.KeyParam"))
    43  		return
    44  	}
    45  	key, keyErr := kp.CacheKey(ctx)
    46  	if keyErr != nil {
    47  		err = errors.Warning("fns: set cache failed").WithCause(keyErr)
    48  		return
    49  	}
    50  	p, encodeErr := avro.Marshal(value)
    51  	if encodeErr != nil {
    52  		err = errors.Warning("fns: set cache failed").WithCause(encodeErr)
    53  		return
    54  	}
    55  	if ttl < 1 {
    56  		err = errors.Warning("fns: set cache failed").WithCause(fmt.Errorf("ttl is invalid"))
    57  		return
    58  	}
    59  	eps := runtime.Endpoints(ctx)
    60  	_, doErr := eps.Request(ctx, endpointName, setFnName, setFnParam{
    61  		Key:   bytex.ToString(key),
    62  		Value: p,
    63  		TTL:   ttl,
    64  	}, services.WithInternalRequest())
    65  	if doErr != nil {
    66  		err = doErr
    67  		return
    68  	}
    69  	return
    70  }
    71  
    72  type setFnParam struct {
    73  	Key   string        `json:"key" avro:"key"`
    74  	Value []byte        `json:"value" avro:"value"`
    75  	TTL   time.Duration `json:"ttl" avro:"ttl"`
    76  }
    77  
    78  type setFn struct {
    79  	store Store
    80  }
    81  
    82  func (fn *setFn) Name() string {
    83  	return string(setFnName)
    84  }
    85  
    86  func (fn *setFn) Internal() bool {
    87  	return true
    88  }
    89  
    90  func (fn *setFn) Readonly() bool {
    91  	return false
    92  }
    93  
    94  func (fn *setFn) Handle(r services.Request) (v interface{}, err error) {
    95  	if !r.Param().Valid() {
    96  		err = errors.Warning("fns: set cache failed").WithCause(errors.Warning("param is invalid"))
    97  		return
    98  	}
    99  	param, paramErr := services.ValueOfParam[setFnParam](r.Param())
   100  	if paramErr != nil {
   101  		err = errors.Warning("fns: set cache failed").WithCause(paramErr)
   102  		return
   103  	}
   104  	key := bytex.FromString(param.Key)
   105  	if len(key) == 0 {
   106  		err = errors.Warning("fns: set cache failed").WithCause(errors.Warning("param is invalid"))
   107  		return
   108  	}
   109  	value := param.Value
   110  	if len(value) == 0 {
   111  		err = errors.Warning("fns: set cache failed").WithCause(errors.Warning("param is invalid"))
   112  		return
   113  	}
   114  	ttl := param.TTL
   115  	if ttl < 1 {
   116  		err = errors.Warning("fns: set cache failed").WithCause(errors.Warning("param is invalid"))
   117  		return
   118  	}
   119  	setErr := fn.store.Set(r, key, value, ttl)
   120  	if setErr != nil {
   121  		err = errors.Warning("fns: set cache failed").WithCause(setErr)
   122  		return
   123  	}
   124  	return
   125  }