gf/g/os/gcache/gcache_cache.go

37 lines
1.0 KiB
Go
Raw Normal View History

// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
package gcache
import (
"gitee.com/johng/gf/g/os/gtimer"
"sync/atomic"
2019-01-03 19:11:54 +08:00
"time"
"unsafe"
)
// 缓存对象。
// 底层只有一个缓存对象,如果需要提高并发性能,可新增缓存对象无锁哈希表,用键名做固定分区。
type Cache struct {
2018-09-18 00:01:10 +08:00
*memCache
}
// Cache对象按照缓存键名首字母做了分组
2018-09-18 00:01:10 +08:00
func New(lruCap...int) *Cache {
c := &Cache {
memCache : newMemCache(lruCap...),
}
gtimer.AddSingleton(time.Second, c.syncEventAndClearExpired)
2018-09-18 00:01:10 +08:00
return c
}
// 清空缓存中的所有数据
func (c *Cache) Clear() {
2018-09-18 00:01:10 +08:00
// 使用原子操作替换缓存对象
old := atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&c.memCache)), unsafe.Pointer(newMemCache()))
// 关闭旧的缓存对象
(*memCache)(old).Close()
}