Files
gf/os/gcache/gcache_cache.go

38 lines
885 B
Go
Raw Normal View History

// Copyright 2018 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
package gcache
import (
2019-06-19 09:06:52 +08:00
"sync/atomic"
"time"
"unsafe"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/os/gtimer"
)
2019-06-10 21:23:49 +08:00
// Cache struct.
type Cache struct {
2019-06-19 09:06:52 +08:00
*memCache
}
2019-06-10 21:23:49 +08:00
// New creates and returns a new cache object.
2019-06-19 09:06:52 +08:00
func New(lruCap ...int) *Cache {
c := &Cache{
memCache: newMemCache(lruCap...),
}
gtimer.AddSingleton(time.Second, c.syncEventAndClearExpired)
return c
}
2019-06-10 21:23:49 +08:00
// Clear clears all data of the cache.
func (c *Cache) Clear() {
2019-06-11 20:57:43 +08:00
// atomic swap to ensure atomicity.
2019-06-19 09:06:52 +08:00
old := atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&c.memCache)), unsafe.Pointer(newMemCache()))
// close the old cache object.
(*memCache)(old).Close()
}