OSDN Git Service

fmtKey
authorHAOYUatHZ <haoyu@protonmail.com>
Wed, 19 Jun 2019 08:26:24 +0000 (16:26 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Wed, 19 Jun 2019 08:26:24 +0000 (16:26 +0800)
federation/database/asset_store.go

index 6019ce5..e5918f1 100644 (file)
@@ -1,6 +1,8 @@
 package database
 
 import (
+       "fmt"
+
        "github.com/golang/groupcache/lru"
        "github.com/jinzhu/gorm"
 
@@ -8,7 +10,20 @@ import (
        "github.com/vapor/federation/database/orm"
 )
 
-const maxAssetCached = 1024
+const (
+       maxAssetCached = 1024
+
+       ormIDPrefix   = "ormID"
+       assetIDPrefix = "assetID"
+)
+
+func fmtOrmIDKey(ormID uint64) string {
+       return fmt.Sprintf("%s:%d", ormIDPrefix, ormID)
+}
+
+func fmtAssetIDKey(assetID string) string {
+       return fmt.Sprintf("%s:%s", assetIDPrefix, assetID)
+}
 
 type AssetStore struct {
        cache *lru.Cache
@@ -22,8 +37,8 @@ func NewAssetStore(db *gorm.DB) *AssetStore {
        }
 }
 
-func (a *AssetStore) GetByOrmID(id uint64) (*orm.Asset, error) {
-       asset := &orm.Asset{ID: id}
+func (a *AssetStore) GetByOrmID(ormID uint64) (*orm.Asset, error) {
+       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")
        }