-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from xufeisofly/feature/cache-for-src-value
feat: add cache for getting src field value
- Loading branch information
Showing
15 changed files
with
603 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package portal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/bluele/gcache" | ||
) | ||
|
||
type Cacher interface { | ||
Set(ctx context.Context, key interface{}, value interface{}) error | ||
Get(ctx context.Context, key interface{}) (interface{}, error) | ||
} | ||
|
||
type LRUCache struct { | ||
c gcache.Cache | ||
} | ||
|
||
func NewLRUCache(size int) *LRUCache { | ||
return &LRUCache{ | ||
c: gcache.New(size).LRU().Build(), | ||
} | ||
} | ||
|
||
var _ Cacher = (*LRUCache)(nil) | ||
|
||
func (lru *LRUCache) Set(_ context.Context, key, value interface{}) error { | ||
return lru.c.Set(key, value) | ||
} | ||
|
||
func (lru *LRUCache) Get(_ context.Context, key interface{}) (interface{}, error) { | ||
return lru.c.Get(key) | ||
} | ||
|
||
const ( | ||
cacheKeyTem = "%s#%s#%s" | ||
defaultLRUSize = 8192 | ||
) | ||
|
||
var ( | ||
DefaultCache = NewLRUCache(defaultLRUSize) | ||
portalCache Cacher | ||
isCacheDisabled = false | ||
) | ||
|
||
// SetCache enable cache strategy | ||
func SetCache(c Cacher) { | ||
if c == nil { | ||
isCacheDisabled = true | ||
return | ||
} | ||
isCacheDisabled = false | ||
portalCache = c | ||
} | ||
|
||
// genCacheKey generate cache key | ||
// rules: ReceiverName#MethodName#cacheID | ||
// eg. meth:GetName UserSchema#GetName#0xc000498150, | ||
// attr:Name UserModel#Name#0xc000498150 | ||
func genCacheKey(ctx context.Context, receiver interface{}, cacheObj interface{}, methodName string) *string { | ||
cacheID := defaultCacheID(cacheObj) | ||
|
||
ck := fmt.Sprintf(cacheKeyTem, structName(receiver), methodName, cacheID) | ||
return &ck | ||
} | ||
|
||
// defaultCacheID is the addr of src struct | ||
func defaultCacheID(cacheObj interface{}) string { | ||
return fmt.Sprintf("%p", cacheObj) | ||
} | ||
|
||
func isCacheKeyValid(cacheKey *string) bool { | ||
return portalCache != nil && cacheKey != nil | ||
} | ||
|
||
type cachable interface { | ||
PortalDisableCache() bool | ||
} |
Oops, something went wrong.