Files
gf/os/gcache/gcache_mem_cache_lru.go

109 lines
2.7 KiB
Go
Raw Normal View History

// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
2018-03-27 17:53:46 +08:00
//
// 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.
2018-03-27 17:53:46 +08:00
package gcache
import (
2019-06-11 20:57:43 +08:00
"time"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/container/glist"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/container/gtype"
"github.com/gogf/gf/os/gtimer"
2018-03-27 17:53:46 +08:00
)
2019-06-11 20:57:43 +08:00
// LRU cache object.
// It uses list.List from stdlib for its underlying doubly linked list.
type memCacheLru struct {
2019-06-19 09:06:52 +08:00
cache *memCache // Parent cache object.
data *gmap.Map // Key mapping to the item of the list.
list *glist.List // Key list.
rawList *glist.List // History for key adding.
closed *gtype.Bool // Closed or not.
2018-03-27 17:53:46 +08:00
}
2019-06-11 20:57:43 +08:00
// newMemCacheLru creates and returns a new LRU object.
func newMemCacheLru(cache *memCache) *memCacheLru {
2019-06-19 09:06:52 +08:00
lru := &memCacheLru{
cache: cache,
data: gmap.New(true),
list: glist.New(true),
rawList: glist.New(true),
2019-06-19 09:06:52 +08:00
closed: gtype.NewBool(),
}
gtimer.AddSingleton(time.Second, lru.SyncAndClear)
return lru
2018-03-27 17:53:46 +08:00
}
2019-06-11 20:57:43 +08:00
// Close closes the LRU object.
func (lru *memCacheLru) Close() {
2019-06-19 09:06:52 +08:00
lru.closed.Set(true)
2018-03-27 17:53:46 +08:00
}
2019-06-11 20:57:43 +08:00
// Remove deletes the <key> FROM <lru>.
func (lru *memCacheLru) Remove(key interface{}) {
2019-06-19 09:06:52 +08:00
if v := lru.data.Get(key); v != nil {
lru.data.Remove(key)
lru.list.Remove(v.(*glist.Element))
}
}
2019-06-11 20:57:43 +08:00
// Size returns the size of <lru>.
2018-09-19 09:47:50 +08:00
func (lru *memCacheLru) Size() int {
2019-06-19 09:06:52 +08:00
return lru.data.Size()
2018-09-19 09:47:50 +08:00
}
2019-06-11 20:57:43 +08:00
// Push pushes <key> to the tail of <lru>.
func (lru *memCacheLru) Push(key interface{}) {
2019-06-19 09:06:52 +08:00
lru.rawList.PushBack(key)
2018-03-27 17:53:46 +08:00
}
2019-06-11 20:57:43 +08:00
// Pop deletes and returns the key from tail of <lru>.
func (lru *memCacheLru) Pop() interface{} {
2019-06-19 09:06:52 +08:00
if v := lru.list.PopBack(); v != nil {
lru.data.Remove(v)
return v
}
return nil
2018-03-27 17:53:46 +08:00
}
2019-06-11 20:57:43 +08:00
// Print is used for test only.
//func (lru *memCacheLru) Print() {
// for _, v := range lru.list.FrontAll() {
// fmt.Printf("%v ", v)
// }
// fmt.Println()
//}
2018-03-27 17:53:46 +08:00
2019-06-11 20:57:43 +08:00
// SyncAndClear synchronizes the keys from <rawList> to <list> and <data>
// using Least Recently Used algorithm.
2018-12-30 14:53:16 +08:00
func (lru *memCacheLru) SyncAndClear() {
2019-06-19 09:06:52 +08:00
if lru.closed.Val() {
gtimer.Exit()
return
}
// Data synchronization.
for {
if v := lru.rawList.PopFront(); v != nil {
// Deleting the key from list.
if v := lru.data.Get(v); v != nil {
lru.list.Remove(v.(*glist.Element))
}
// Pushing key to the head of the list
// and setting its list item to hash table for quick indexing.
lru.data.Set(v, lru.list.PushFront(v))
} else {
break
}
}
// Data cleaning up.
for i := lru.Size() - lru.cache.cap; i > 0; i-- {
if s := lru.Pop(); s != nil {
lru.cache.clearByKey(s, true)
}
}
}