github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/services/caches/remove.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/errors" 23 "github.com/aacfactory/fns/commons/bytex" 24 "github.com/aacfactory/fns/context" 25 "github.com/aacfactory/fns/runtime" 26 "github.com/aacfactory/fns/services" 27 ) 28 29 func Remove(ctx context.Context, param interface{}) (err error) { 30 if param == nil { 31 err = errors.Warning("fns: remove cache failed").WithCause(fmt.Errorf("param is nil")) 32 return 33 } 34 kp, ok := param.(KeyParam) 35 if !ok { 36 err = errors.Warning("fns: remove cache failed").WithCause(fmt.Errorf("param dose not implement caches.KeyParam")) 37 return 38 } 39 key, keyErr := kp.CacheKey(ctx) 40 if keyErr != nil { 41 err = errors.Warning("fns: remove cache failed").WithCause(keyErr) 42 return 43 } 44 eps := runtime.Endpoints(ctx) 45 _, doErr := eps.Request(ctx, endpointName, remFnName, removeFnParam{ 46 Key: bytex.ToString(key), 47 }, services.WithInternalRequest()) 48 if doErr != nil { 49 err = doErr 50 return 51 } 52 return 53 } 54 55 type removeFnParam struct { 56 Key string `json:"key" avro:"key"` 57 } 58 59 type removeFn struct { 60 store Store 61 } 62 63 func (fn *removeFn) Name() string { 64 return string(remFnName) 65 } 66 67 func (fn *removeFn) Internal() bool { 68 return true 69 } 70 71 func (fn *removeFn) Readonly() bool { 72 return false 73 } 74 75 func (fn *removeFn) Handle(r services.Request) (v interface{}, err error) { 76 if !r.Param().Valid() { 77 err = errors.Warning("fns: remove cache failed").WithCause(errors.Warning("param is invalid")) 78 return 79 } 80 param, paramErr := services.ValueOfParam[removeFnParam](r.Param()) 81 if paramErr != nil { 82 err = errors.Warning("fns: remove cache failed").WithCause(paramErr) 83 return 84 } 85 key := bytex.FromString(param.Key) 86 if len(key) == 0 { 87 err = errors.Warning("fns: remove cache failed").WithCause(errors.Warning("param is invalid")) 88 return 89 } 90 removeErr := fn.store.Remove(r, key) 91 if removeErr != nil { 92 err = errors.Warning("fns: remove cache failed").WithCause(removeErr) 93 return 94 } 95 return 96 }