github.com/vicanso/pike@v1.0.1-0.20210630235453-9099e041f6ec/cache/cache.go (about) 1 // MIT License 2 3 // Copyright (c) 2020 Tree Xie 4 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 23 package cache 24 25 import ( 26 "bytes" 27 "encoding/binary" 28 "time" 29 "unsafe" 30 31 "github.com/vicanso/pike/config" 32 ) 33 34 // byteSliceToString converts a []byte to string without a heap allocation. 35 func byteSliceToString(b []byte) string { 36 return *(*string)(unsafe.Pointer(&b)) 37 } 38 39 // uint32ToBytes convert int to uint32 and convert to bytes 40 func uint32ToBytes(value int) []byte { 41 buf := make([]byte, 4) 42 binary.BigEndian.PutUint32(buf, uint32(value)) 43 return buf 44 } 45 46 // readUint32ToInt read uint32 from bytes and convert to int 47 func readUint32ToInt(buffer *bytes.Buffer) (int, error) { 48 var value uint32 49 err := binary.Read(buffer, binary.BigEndian, &value) 50 if err != nil { 51 return 0, err 52 } 53 return int(value), nil 54 } 55 56 // uint64ToBytes convert int64 to uint64 and covert to bytes 57 func uint64ToBytes(value int64) []byte { 58 buf := make([]byte, 8) 59 binary.BigEndian.PutUint64(buf, uint64(value)) 60 return buf 61 } 62 63 // readUint64 read uint64 from bytes and convert to int64 64 func readUint64ToInt64(buffer *bytes.Buffer) (int64, error) { 65 var value uint64 66 err := binary.Read(buffer, binary.BigEndian, &value) 67 if err != nil { 68 return 0, err 69 } 70 return int64(value), nil 71 } 72 73 var defaultDispatchers = NewDispatchers(nil) 74 75 // GetDispatcher get dispatcher form default dispatchers 76 func GetDispatcher(name string) *dispatcher { 77 return defaultDispatchers.Get(name) 78 } 79 80 // RemoveHTTPCache remove http cache form default dispatchers 81 func RemoveHTTPCache(name string, key []byte) { 82 defaultDispatchers.RemoveHTTPCache(name, key) 83 } 84 85 func convertConfigs(configs []config.CacheConfig) []DispatcherOption { 86 opts := make([]DispatcherOption, 0) 87 for _, item := range configs { 88 d, _ := time.ParseDuration(item.HitForPass) 89 opts = append(opts, DispatcherOption{ 90 Name: item.Name, 91 Size: item.Size, 92 HitForPass: int(d.Seconds()), 93 Store: item.Store, 94 }) 95 } 96 return opts 97 } 98 99 // ResetDispatchers reset default dispatchers 100 func ResetDispatchers(configs []config.CacheConfig) { 101 defaultDispatchers.Reset(convertConfigs(configs)) 102 }