diff --git a/list.go b/list.go index cd856df..a549993 100644 --- a/list.go +++ b/list.go @@ -1,17 +1,23 @@ package cacheMemory -type List[T any] struct { +type CacheList[T any] struct { items map[string]*T } +func NewCache[T any]() CacheList[T] { + return CacheList[T]{ + items: make(map[string]*T), + } +} + // SetIfNotExists 写入缓存,如果Key不存在 -func (receiver *List[T]) SetIfNotExists(key string, getItem func() *T) { +func (receiver *CacheList[T]) SetIfNotExists(key string, getItem func() *T) { if _, isExists := receiver.items[key]; !isExists { receiver.items[key] = getItem() } } // Get 获取数据 -func (receiver *List[T]) Get(key string) *T { +func (receiver *CacheList[T]) Get(key string) *T { return receiver.items[key] }