UOE-7910 Added Fetcher Module
This commit is contained in:
parent
bf7da175d3
commit
477f2fc5ea
36
fetcher.go
36
fetcher.go
|
@ -1,23 +1,29 @@
|
||||||
package go-cache
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
errInvalidKey = "type:[invalid_key] key:[%s]"
|
errInvalidKey = "type:[invalid_key] key:[%s]"
|
||||||
)
|
)
|
||||||
type callbackFunc func(key string)(interface{},error)
|
|
||||||
|
type callbackFunc func(key string) (interface{}, error)
|
||||||
|
|
||||||
type fetcher struct {
|
type fetcher struct {
|
||||||
cb map[string]callbackFunc
|
cb map[string]callbackFunc
|
||||||
prefixLen int
|
prefixLen int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFetcher(prefixLen int) *fetcher {
|
func NewFetcher(prefixLen int) *fetcher {
|
||||||
return &fetcher{
|
return &fetcher{
|
||||||
cb : make(map[string]callbackFunc),
|
cb: make(map[string]callbackFunc),
|
||||||
prefixLen: prefixLen,
|
prefixLen: prefixLen,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fetcher) Register(keyPrefix string, cbf callbackFunc) bool{
|
func (f *fetcher) Register(keyPrefix string, cbf callbackFunc) bool {
|
||||||
if len(keyPrefix) != f.prefixLen {
|
if len(keyPrefix) != f.prefixLen {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -25,31 +31,30 @@ func (f *fetcher) Register(keyPrefix string, cbf callbackFunc) bool{
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fetcher) execute(key string) (interface{},error) {
|
func (f *fetcher) Execute(key string) (interface{}, error) {
|
||||||
if len(key) < f.prefixLen {
|
if len(key) < f.prefixLen {
|
||||||
return nil, fmt.Errorf(errInvalidKey,key)
|
return nil, errors.New("Invalid Key " + key)
|
||||||
}
|
}
|
||||||
keyPrefix := key[:f.prefixLen]
|
keyPrefix := key[:f.prefixLen]
|
||||||
cbf, ok := f.cb[keyPrefix]
|
cbf, ok := f.cb[keyPrefix]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf(errInvalidKey,key)
|
return nil, fmt.Errorf(errInvalidKey, key)
|
||||||
}
|
}
|
||||||
return cbf(key)
|
return cbf(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
//header bidding
|
//header bidding
|
||||||
sample keys
|
sample keys
|
||||||
|
|
||||||
Key_Prefix_PUB_SLOT_INFO = "AAA00"
|
Key_Prefix_PUB_SLOT_INFO = "AAA00" -> DB
|
||||||
Key_Prefix_PUB_HB_PARTNER = "AAB00"
|
Key_Prefix_PUB_HB_PARTNER = "AAB00"
|
||||||
Key_Prefix_PubAdunitConfig = "AAC00"
|
Key_Prefix_PubAdunitConfig = "AAC00"
|
||||||
Key_Prefix_PubSlotHashInfo = "AAD00"
|
Key_Prefix_PubSlotHashInfo = "AAD00"
|
||||||
Key_Prefix_PubSlotRegex = "AAE00"
|
Key_Prefix_PubSlotRegex = "AAE00"
|
||||||
Key_Prefix_PubSlotNameHash = "AAF00"
|
Key_Prefix_PubSlotNameHash = "AAF00"
|
||||||
Key_Prefix_PubVASTTags = "AAG00"
|
Key_Prefix_PubVASTTags = "AAG00" -> DBPubVasttags(key string) (interface,error)
|
||||||
|
|
||||||
|
|
||||||
PUB_SLOT_INFO = Key_Prefix_PUB_SLOT_INFO + "_%d_%d_%d_%d" // publisher slot mapping at publisher, profile, display version and adapter level
|
PUB_SLOT_INFO = Key_Prefix_PUB_SLOT_INFO + "_%d_%d_%d_%d" // publisher slot mapping at publisher, profile, display version and adapter level
|
||||||
|
@ -64,6 +69,7 @@ func DBGetVASTTags(key string) (interface{},error) {
|
||||||
strings.Split(key,"_")
|
strings.Split(key,"_")
|
||||||
key[0] // keyprefix
|
key[0] // keyprefix
|
||||||
key[1] //publisherid
|
key[1] //publisherid
|
||||||
|
return GetVastTag(rtb req, pub ID, comn etc)
|
||||||
}
|
}
|
||||||
|
|
||||||
//hb
|
//hb
|
||||||
|
@ -75,10 +81,10 @@ type AsyncCache struct {
|
||||||
ks *keystatus
|
ks *keystatus
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AsycCache) aget(key string) {
|
func (ac *AsyncCache) aget(key string) {
|
||||||
data, err := c.f.execute(key)
|
data, err := ac.f.execute(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.c.set(key, data)
|
ac.c.set(key, data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -0,0 +1,90 @@
|
||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_fetcher_Execute(t *testing.T) {
|
||||||
|
type fields struct {
|
||||||
|
cb map[string]callbackFunc
|
||||||
|
prefixLen int
|
||||||
|
}
|
||||||
|
type args struct {
|
||||||
|
key string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
fields fields
|
||||||
|
args args
|
||||||
|
want interface{}
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
// TODO: Add test cases.
|
||||||
|
{
|
||||||
|
name: "Invalid Key",
|
||||||
|
fields: fields{
|
||||||
|
cb: map[string]callbackFunc{
|
||||||
|
"AAG00": nil,
|
||||||
|
},
|
||||||
|
prefixLen: 5,
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
key: "INV",
|
||||||
|
},
|
||||||
|
want: nil,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Unexisting Key Execution",
|
||||||
|
fields: fields{
|
||||||
|
cb: map[string]callbackFunc{
|
||||||
|
"AAG00": nil,
|
||||||
|
"AAA00": nil,
|
||||||
|
"AAB00": nil,
|
||||||
|
},
|
||||||
|
prefixLen: 5,
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
key: "UnExisted_Key",
|
||||||
|
},
|
||||||
|
want: nil,
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Valid Key Execution",
|
||||||
|
fields: fields{
|
||||||
|
cb: make(map[string]callbackFunc),
|
||||||
|
prefixLen: 5,
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
key: "AAG00_5890",
|
||||||
|
},
|
||||||
|
want: f.cb{
|
||||||
|
"afewv",
|
||||||
|
},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
f := &fetcher{
|
||||||
|
cb: tt.fields.cb,
|
||||||
|
prefixLen: tt.fields.prefixLen,
|
||||||
|
}
|
||||||
|
got, err := f.Execute(tt.args.key)
|
||||||
|
// if errors.Is(err, tt.wantErr) {
|
||||||
|
// t.Errorf("fetcher.Execute() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("fetcher.Execute() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(got, tt.want) {
|
||||||
|
t.Errorf("fetcher.Execute() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue