"github.com/vapor/federation/database/orm"
)
-// TODO:
+const maxAssetCached = 1024
+
type AssetKeeper struct {
- db *gorm.DB
- assetCache *AssetCache
+ cache *lru.Cache
+ db *gorm.DB
}
func NewAssetKeeper(db *gorm.DB) *AssetKeeper {
return &AssetKeeper{
- db: db,
- assetCache: NewAssetCache(),
+ cache: lru.New(maxAssetCached),
+ db: db,
}
}
}
func (a *AssetKeeper) Get(assetID string) (*orm.Asset, error) {
- if asset := a.assetCache.Get(assetID); asset != nil {
- return asset, nil
+ if v, ok := a.cache.Get(assetID); ok {
+ return v.(*orm.Asset), nil
}
asset := &orm.Asset{AssetID: assetID}
return nil, errors.Wrap(err, "asset not found in memory and mysql")
}
- a.assetCache.Add(assetID, asset)
+ a.cache.Add(assetID, asset)
return asset, nil
}
return err
}
- a.assetCache.Add(asset.AssetID, asset)
+ a.cache.Add(asset.AssetID, asset)
return nil
}
-
-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)
-}