|
|
@ -48,13 +48,64 @@ type Obj interface { |
|
|
|
LinkAdd(ctx context.Context, element, from, to string) (result models.ResponseData, err error) |
|
|
|
LinkDelete(ctx context.Context, element, from, to string) (result models.ResponseData, err error) |
|
|
|
LinkGetWithCache(ctx context.Context, tpl, obj, mode, short string) (result models.ResponseData, err error) |
|
|
|
Query(ctx context.Context, query, method, bodyJSON string) (result string, err error) |
|
|
|
Search(ctx context.Context, query, method, bodyJSON string) (resp string, err error) |
|
|
|
SearchWithCache(ctx context.Context, query, method, bodyJSON string) (resp string, err error) |
|
|
|
Query(ctx context.Context, query, method, bodyJSON string) (result string, err error) |
|
|
|
QueryWithCache(ctx context.Context, query, method, bodyJSON string) (result string, err error) |
|
|
|
Element(ctx context.Context, action, body string) (result models.ResponseData, err error) |
|
|
|
ElementWithCache(ctx context.Context, action, body string) (result models.ResponseData, err error) |
|
|
|
} |
|
|
|
|
|
|
|
// Search результат выводим в объект как при вызове Curl
|
|
|
|
func (a *api) Search(ctx context.Context, query, method, bodyJSON string) (resp string, err error) { |
|
|
|
resp, err = a.search(ctx, query, method, bodyJSON) |
|
|
|
if err != nil { |
|
|
|
logger.Error(ctx, "error UpdateFilter primary haproxy", zap.Error(err)) |
|
|
|
return resp, fmt.Errorf("error request Search (primary route). err: %s", err) |
|
|
|
} |
|
|
|
|
|
|
|
return resp, nil |
|
|
|
} |
|
|
|
|
|
|
|
// SearchWithCache результат выводим в объект как при вызове Curl
|
|
|
|
// (с кешем если задан TTL кеширования при инициализации кеша)
|
|
|
|
func (a *api) SearchWithCache(ctx context.Context, query, method, bodyJSON string) (result string, err error) { |
|
|
|
|
|
|
|
//return a.Search(ctx, query, method, bodyJSON)
|
|
|
|
|
|
|
|
var handlers = map[string]string{} |
|
|
|
handlers[headerRequestId] = logger.GetRequestIDCtx(ctx) |
|
|
|
if a.observeLog { |
|
|
|
defer a.observeLogger(ctx, time.Now(), "QueryWithCache", err, query, method, bodyJSON) |
|
|
|
} |
|
|
|
key := lib.Hash(fmt.Sprintf("%s%s%s", query, method, bodyJSON)) |
|
|
|
|
|
|
|
cacheValue, err := cache.Cache().Get(key) |
|
|
|
|
|
|
|
if errors.Is(err, cache.ErrorKeyNotFound) { |
|
|
|
var value interface{} |
|
|
|
value, err = cache.Cache().Upsert(key, func() (res interface{}, err error) { |
|
|
|
res, err = a.Search(ctx, query, method, bodyJSON) |
|
|
|
return res, err |
|
|
|
}, a.cacheUpdateInterval) |
|
|
|
if err == nil && value != nil { |
|
|
|
return fmt.Sprint(value), nil |
|
|
|
} else { |
|
|
|
err = fmt.Errorf("error exec cache query (Query). err: %s, value is empty: %t, value: %+v", err, value == nil, value) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
logger.Error(ctx, "error exec cache query", zap.String("func", "QueryWithCache"), zap.String("key", key), zap.Error(err)) |
|
|
|
cacheValue, err = a.Search(ctx, query, method, bodyJSON) |
|
|
|
if err != nil { |
|
|
|
return result, fmt.Errorf("error get cache (Query). err: %s", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return fmt.Sprint(cacheValue), err |
|
|
|
} |
|
|
|
|
|
|
|
// Query результат выводим в объект как при вызове Curl
|
|
|
|
func (a *api) Query(ctx context.Context, query, method, bodyJSON string) (result string, err error) { |
|
|
|
//_, err = a.cb.Execute(func() (interface{}, error) {
|
|
|
@ -69,17 +120,6 @@ func (a *api) Query(ctx context.Context, query, method, bodyJSON string) (result |
|
|
|
return result, err |
|
|
|
} |
|
|
|
|
|
|
|
// Search результат выводим в объект как при вызове Curl
|
|
|
|
func (a *api) Search(ctx context.Context, query, method, bodyJSON string) (resp string, err error) { |
|
|
|
resp, err = a.search(ctx, query, method, bodyJSON) |
|
|
|
if err != nil { |
|
|
|
logger.Error(ctx, "error UpdateFilter primary haproxy", zap.Error(err)) |
|
|
|
return resp, fmt.Errorf("error request Search (primary route). err: %s", err) |
|
|
|
} |
|
|
|
|
|
|
|
return resp, nil |
|
|
|
} |
|
|
|
|
|
|
|
// QueryWithCache результат выводим в объект как при вызове Curl
|
|
|
|
// (с кешем если задан TTL кеширования при инициализации кеша)
|
|
|
|
func (a *api) QueryWithCache(ctx context.Context, query, method, bodyJSON string) (result string, err error) { |
|
|
|