Skip to content

Commit

Permalink
新增:本地缓存cacheMemory.List
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Jul 11, 2024
1 parent 1c7dd0b commit 3a64d4f
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions list.go
Original file line number Diff line number Diff line change
@@ -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]
}

0 comments on commit 3a64d4f

Please sign in to comment.