OSDN Git Service

fix cache query
authorHAOYUatHZ <haoyu@protonmail.com>
Wed, 19 Jun 2019 08:32:22 +0000 (16:32 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Wed, 19 Jun 2019 08:32:22 +0000 (16:32 +0800)
federation/database/asset_store.go

index e5918f1..6c32e95 100644 (file)
@@ -38,16 +38,22 @@ func NewAssetStore(db *gorm.DB) *AssetStore {
 }
 
 func (a *AssetStore) GetByOrmID(ormID uint64) (*orm.Asset, error) {
+       if v, ok := a.cache.Get(fmtOrmIDKey(ormID)); ok {
+               return v.(*orm.Asset), nil
+       }
+
        asset := &orm.Asset{ID: ormID}
        if err := a.db.Where(asset).First(asset).Error; err != nil {
                return nil, errors.Wrap(err, "asset not found by orm id")
        }
 
+       a.cache.Add(fmtOrmIDKey(asset.ID), asset)
+       a.cache.Add(fmtAssetIDKey(asset.AssetID), asset)
        return asset, nil
 }
 
 func (a *AssetStore) GetByAssetID(assetID string) (*orm.Asset, error) {
-       if v, ok := a.cache.Get(assetID); ok {
+       if v, ok := a.cache.Get(fmtAssetIDKey(assetID)); ok {
                return v.(*orm.Asset), nil
        }
 
@@ -56,7 +62,8 @@ func (a *AssetStore) GetByAssetID(assetID string) (*orm.Asset, error) {
                return nil, errors.Wrap(err, "asset not found in memory and mysql")
        }
 
-       a.cache.Add(assetID, asset)
+       a.cache.Add(fmtOrmIDKey(asset.ID), asset)
+       a.cache.Add(fmtAssetIDKey(asset.AssetID), asset)
        return asset, nil
 }
 
@@ -65,6 +72,7 @@ func (a *AssetStore) Add(asset *orm.Asset) error {
                return err
        }
 
-       a.cache.Add(asset.AssetID, asset)
+       a.cache.Add(fmtOrmIDKey(asset.ID), asset)
+       a.cache.Add(fmtAssetIDKey(asset.AssetID), asset)
        return nil
 }