代码拉取完成,页面将自动刷新
package main
import "sync"
type SimpleGenericCache[K Ordered, V any] struct { // 使用 Ordered
cache map[K]V
mu sync.RWMutex
}
/*简单缓存------------------*/
//对象初始化
func NewSimpleGenericCache[K Ordered, V any]() (*SimpleGenericCache[K, V], error) {
if err := checkGoVersionCanUseGenerics(); err != nil {
return nil, err
}
return &SimpleGenericCache[K, V]{
cache: make(map[K]V),
}, nil
}
// 设置缓存项
func (c *SimpleGenericCache[K, V]) Set(key K, value V) {
c.mu.Lock()
defer c.mu.Unlock()
c.cache[key] = value
}
// 获取缓存项
func (c *SimpleGenericCache[K, V]) Get(key K) (V, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
value, ok := c.cache[key]
return value, ok
}
// 删除某个Key
func (c *SimpleGenericCache[K, V]) Del(key K) {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.cache, key)
}
// ShowAllKey 获取所有缓存key
func (c *SimpleGenericCache[K, V]) ShowAllKey() []K {
keys := make([]K, 0)
c.mu.RLock()
defer c.mu.RUnlock()
for key := range c.cache {
keys = append(keys, key)
}
return keys
}
func (c *SimpleGenericCache[K, V]) UnSafeRlock() {
c.mu.RLock()
}
func (c *SimpleGenericCache[K, V]) UnSafeRUnlock() {
c.mu.RUnlock()
}
func (c *SimpleGenericCache[K, V]) UnSafeLock() {
c.mu.Lock()
}
func (c *SimpleGenericCache[K, V]) UnSafeUnLock() {
c.mu.Unlock()
}
func (c *SimpleGenericCache[K, V]) UnSafeGet(key K) (V, bool) {
value, ok := c.cache[key]
return value, ok
}
func (c *SimpleGenericCache[K, V]) UnSafeSet(key K, value V) {
c.cache[key] = value
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。