abstracted cache to an interface (#535)

This commit is contained in:
dk 2020-07-03 18:23:44 -07:00 committed by GitHub
parent de116e8367
commit 4c3ff12cba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 32 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package d2common
import (
"errors"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"log"
"sync"
)
@ -14,6 +15,7 @@ type cacheNode struct {
weight int
}
// Cache stores arbitrary data for fast retrieval
type Cache struct {
head *cacheNode
tail *cacheNode
@ -24,22 +26,27 @@ type Cache struct {
mutex sync.Mutex
}
func CreateCache(budget int) *Cache {
// CreateCache creates an instance of a Cache
func CreateCache(budget int) d2interface.Cache {
return &Cache{lookup: make(map[string]*cacheNode), budget: budget}
}
// SetVerbose turns on verbose printing (warnings and stuff)
func (c *Cache) SetVerbose(verbose bool) {
c.verbose = verbose
}
// GetWeight gets the "weight" of a cache
func (c *Cache) GetWeight() int {
return c.weight
}
// GetBudget gets the memory budget of a cache
func (c *Cache) GetBudget() int {
return c.budget
}
// Insert inserts an object into the cache
func (c *Cache) Insert(key string, value interface{}, weight int) error {
c.mutex.Lock()
defer c.mutex.Unlock()
@ -88,6 +95,7 @@ func (c *Cache) Insert(key string, value interface{}, weight int) error {
return nil
}
// Retrieve gets an object out of the cache
func (c *Cache) Retrieve(key string) (interface{}, bool) {
c.mutex.Lock()
defer c.mutex.Unlock()
@ -122,6 +130,7 @@ func (c *Cache) Retrieve(key string) (interface{}, bool) {
return node.value, true
}
// Clear removes all cache entries
func (c *Cache) Clear() {
c.mutex.Lock()
defer c.mutex.Unlock()

View File

@ -0,0 +1,11 @@
package d2interface
// Cache stores arbitrary data for fast retrieval
type Cache interface {
SetVerbose(verbose bool)
GetWeight() int
GetBudget() int
Insert(key string, value interface{}, weight int) error
Retrieve(key string) (interface{}, bool)
Clear()
}

View File

@ -15,7 +15,7 @@ const (
)
type animationManager struct {
cache *d2common.Cache
cache d2interface.Cache
renderer d2interface.Renderer
}

View File

@ -2,6 +2,7 @@ package d2asset
import (
"errors"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"path"
"sync"
@ -16,7 +17,7 @@ type archiveEntry struct {
}
type archiveManager struct {
cache *d2common.Cache
cache d2interface.Cache
config *d2config.Configuration
entries []archiveEntry
mutex sync.Mutex

View File

@ -2,6 +2,7 @@ package d2asset
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2mpq"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"strings"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
@ -14,7 +15,7 @@ const (
)
type fileManager struct {
cache *d2common.Cache
cache d2interface.Cache
archiveManager *archiveManager
config *d2config.Configuration
}

View File

@ -2,6 +2,7 @@ package d2asset
import (
"fmt"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
)
@ -11,7 +12,7 @@ const (
)
type fontManager struct {
cache *d2common.Cache
cache d2interface.Cache
}
func createFontManager() *fontManager {

View File

@ -3,10 +3,11 @@ package d2asset
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dat"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
)
type paletteManager struct {
cache *d2common.Cache
cache d2interface.Cache
}
const (

View File

@ -3,10 +3,11 @@ package d2asset
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2pl2"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
)
type paletteTransformManager struct {
cache *d2common.Cache
cache d2interface.Cache
}
const (