github.com/confluentinc/confluent-kafka-go@v1.9.2/schemaregistry/cache/mapcache.go (about) 1 /** 2 * Copyright 2022 Confluent Inc. 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 package cache 18 19 // MapCache is a cache backed by a map 20 type MapCache struct { 21 entries map[interface{}]interface{} 22 } 23 24 // NewMapCache creates a new cache backed by a map 25 func NewMapCache() *MapCache { 26 c := new(MapCache) 27 c.entries = make(map[interface{}]interface{}) 28 return c 29 } 30 31 // Get returns the cache value associated with key 32 // 33 // Parameters: 34 // * `key` - the key to retrieve 35 // 36 // Returns the value associated with key and a bool that is `false` 37 // if the key was not found 38 func (c *MapCache) Get(key interface{}) (value interface{}, ok bool) { 39 value, ok = c.entries[key] 40 return 41 } 42 43 // Put puts a value in cache associated with key 44 // 45 // Parameters: 46 // * `key` - the key to put 47 // * `value` - the value to put 48 func (c *MapCache) Put(key interface{}, value interface{}) { 49 c.entries[key] = value 50 } 51 52 // Delete deletes the cache entry associated with key 53 // 54 // Parameters: 55 // * `key` - the key to delete 56 func (c *MapCache) Delete(key interface{}) { 57 delete(c.entries, key) 58 } 59 60 // ToMap returns the current cache entries copied into a map 61 func (c *MapCache) ToMap() map[interface{}]interface{} { 62 ret := make(map[interface{}]interface{}) 63 for k, v := range c.entries { 64 ret[k] = v 65 } 66 return ret 67 }