mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2025-02-13 20:16:41 -05:00
abstracted cache to an interface (#535)
This commit is contained in:
parent
de116e8367
commit
4c3ff12cba
@ -2,6 +2,7 @@ package d2common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
||||||
"log"
|
"log"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@ -14,6 +15,7 @@ type cacheNode struct {
|
|||||||
weight int
|
weight int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cache stores arbitrary data for fast retrieval
|
||||||
type Cache struct {
|
type Cache struct {
|
||||||
head *cacheNode
|
head *cacheNode
|
||||||
tail *cacheNode
|
tail *cacheNode
|
||||||
@ -24,22 +26,27 @@ type Cache struct {
|
|||||||
mutex sync.Mutex
|
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}
|
return &Cache{lookup: make(map[string]*cacheNode), budget: budget}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetVerbose turns on verbose printing (warnings and stuff)
|
||||||
func (c *Cache) SetVerbose(verbose bool) {
|
func (c *Cache) SetVerbose(verbose bool) {
|
||||||
c.verbose = verbose
|
c.verbose = verbose
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetWeight gets the "weight" of a cache
|
||||||
func (c *Cache) GetWeight() int {
|
func (c *Cache) GetWeight() int {
|
||||||
return c.weight
|
return c.weight
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetBudget gets the memory budget of a cache
|
||||||
func (c *Cache) GetBudget() int {
|
func (c *Cache) GetBudget() int {
|
||||||
return c.budget
|
return c.budget
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Insert inserts an object into the cache
|
||||||
func (c *Cache) Insert(key string, value interface{}, weight int) error {
|
func (c *Cache) Insert(key string, value interface{}, weight int) error {
|
||||||
c.mutex.Lock()
|
c.mutex.Lock()
|
||||||
defer c.mutex.Unlock()
|
defer c.mutex.Unlock()
|
||||||
@ -88,6 +95,7 @@ func (c *Cache) Insert(key string, value interface{}, weight int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Retrieve gets an object out of the cache
|
||||||
func (c *Cache) Retrieve(key string) (interface{}, bool) {
|
func (c *Cache) Retrieve(key string) (interface{}, bool) {
|
||||||
c.mutex.Lock()
|
c.mutex.Lock()
|
||||||
defer c.mutex.Unlock()
|
defer c.mutex.Unlock()
|
||||||
@ -122,6 +130,7 @@ func (c *Cache) Retrieve(key string) (interface{}, bool) {
|
|||||||
return node.value, true
|
return node.value, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clear removes all cache entries
|
||||||
func (c *Cache) Clear() {
|
func (c *Cache) Clear() {
|
||||||
c.mutex.Lock()
|
c.mutex.Lock()
|
||||||
defer c.mutex.Unlock()
|
defer c.mutex.Unlock()
|
||||||
|
11
d2common/d2interface/cache.go
Normal file
11
d2common/d2interface/cache.go
Normal 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()
|
||||||
|
}
|
@ -15,7 +15,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type animationManager struct {
|
type animationManager struct {
|
||||||
cache *d2common.Cache
|
cache d2interface.Cache
|
||||||
renderer d2interface.Renderer
|
renderer d2interface.Renderer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package d2asset
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
||||||
"path"
|
"path"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@ -16,7 +17,7 @@ type archiveEntry struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type archiveManager struct {
|
type archiveManager struct {
|
||||||
cache *d2common.Cache
|
cache d2interface.Cache
|
||||||
config *d2config.Configuration
|
config *d2config.Configuration
|
||||||
entries []archiveEntry
|
entries []archiveEntry
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
|
@ -2,6 +2,7 @@ package d2asset
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2mpq"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2mpq"
|
||||||
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
||||||
@ -14,7 +15,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type fileManager struct {
|
type fileManager struct {
|
||||||
cache *d2common.Cache
|
cache d2interface.Cache
|
||||||
archiveManager *archiveManager
|
archiveManager *archiveManager
|
||||||
config *d2config.Configuration
|
config *d2config.Configuration
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package d2asset
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
||||||
|
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
||||||
)
|
)
|
||||||
@ -11,7 +12,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type fontManager struct {
|
type fontManager struct {
|
||||||
cache *d2common.Cache
|
cache d2interface.Cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func createFontManager() *fontManager {
|
func createFontManager() *fontManager {
|
||||||
|
@ -3,10 +3,11 @@ package d2asset
|
|||||||
import (
|
import (
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dat"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dat"
|
||||||
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
||||||
)
|
)
|
||||||
|
|
||||||
type paletteManager struct {
|
type paletteManager struct {
|
||||||
cache *d2common.Cache
|
cache d2interface.Cache
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -3,10 +3,11 @@ package d2asset
|
|||||||
import (
|
import (
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2pl2"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2pl2"
|
||||||
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
||||||
)
|
)
|
||||||
|
|
||||||
type paletteTransformManager struct {
|
type paletteTransformManager struct {
|
||||||
cache *d2common.Cache
|
cache d2interface.Cache
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
Loading…
Reference in New Issue
Block a user