OSDN Git Service

Merge pull request #201 from Bytom/v0.1
[bytom/vapor.git] / federation / database / cache.go
diff --git a/federation/database/cache.go b/federation/database/cache.go
new file mode 100644 (file)
index 0000000..8a4807d
--- /dev/null
@@ -0,0 +1,33 @@
+package database
+
+import (
+       "github.com/golang/groupcache/lru"
+
+       "github.com/vapor/federation/database/orm"
+)
+
+const maxAssetCached = 1024
+
+type AssetCache struct {
+       lruCache *lru.Cache
+}
+
+func NewAssetCache() *AssetCache {
+       return &AssetCache{lruCache: lru.New(maxAssetCached)}
+}
+
+func (a *AssetCache) Add(assetID string, asset *orm.Asset) {
+       a.lruCache.Add(assetID, asset)
+}
+
+func (a *AssetCache) Get(assetID string) *orm.Asset {
+       if v, ok := a.lruCache.Get(assetID); ok {
+               return v.(*orm.Asset)
+       }
+
+       return nil
+}
+
+func (a *AssetCache) Remove(assetID string) {
+       a.lruCache.Remove(assetID)
+}