-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae155e7
commit 6129918
Showing
17 changed files
with
1,085 additions
and
33 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
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,111 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"database/sql/driver" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type ( | ||
queryKey struct{} | ||
stmtKey struct{} | ||
argsKey struct{} | ||
queryerCtxKey struct{} | ||
namedValueArgsKey struct{} | ||
) | ||
|
||
func ExportMetrics() string { | ||
res := "" | ||
for query, cache := range caches { | ||
stats := cache.cache.Stats() | ||
progress := "[" | ||
for i := 0; i < 20; i++ { | ||
if i < int(stats.HitRatio()*20) { | ||
progress += "#" | ||
} else { | ||
progress += "-" | ||
} | ||
} | ||
statsStr := fmt.Sprintf("%s (%.2f%% - %d/%d) (%d replace) (size %d)", progress, stats.HitRatio()*100, stats.Hits, stats.Misses+stats.Hits, stats.Replacements, stats.Size) | ||
res += fmt.Sprintf("query: \"%s\"\n%s\n\n", query, statsStr) | ||
} | ||
return res | ||
} | ||
|
||
type CacheStats struct { | ||
Query string | ||
HitRatio float64 | ||
Hits int | ||
Misses int | ||
} | ||
|
||
func ExportCacheStats() map[string]CacheStats { | ||
res := make(map[string]CacheStats) | ||
for query, cache := range caches { | ||
stats := cache.cache.Stats() | ||
res[query] = CacheStats{ | ||
Query: query, | ||
HitRatio: stats.HitRatio(), | ||
Hits: int(stats.Hits), | ||
Misses: int(stats.Misses), | ||
} | ||
} | ||
return res | ||
} | ||
|
||
func PurgeAllCaches() { | ||
for _, cache := range caches { | ||
cache.cache.Purge() | ||
} | ||
} | ||
|
||
func cacheName(query string) string { | ||
return query | ||
} | ||
|
||
func cacheKey(args []driver.Value) string { | ||
var b strings.Builder | ||
for _, arg := range args { | ||
switch v := arg.(type) { | ||
case string: | ||
b.WriteString(v) | ||
case []byte: | ||
b.Write(v) | ||
default: | ||
fmt.Fprintf(&b, "%v", v) | ||
} | ||
// delimiter | ||
b.WriteByte(0) | ||
} | ||
return b.String() | ||
} | ||
|
||
func replaceFn(ctx context.Context, key string) (*cacheRows, error) { | ||
queryerCtx, ok := ctx.Value(queryerCtxKey{}).(driver.QueryerContext) | ||
if ok { | ||
query := ctx.Value(queryKey{}).(string) | ||
nvargs := ctx.Value(namedValueArgsKey{}).([]driver.NamedValue) | ||
rows, err := queryerCtx.QueryContext(ctx, query, nvargs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cacheRows, err := newCacheRows(rows) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cacheRows.clone(), nil | ||
} | ||
|
||
stmt := ctx.Value(stmtKey{}).(*customCacheStatement) | ||
args := ctx.Value(argsKey{}).([]driver.Value) | ||
rows, err := stmt.inner.Query(args) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cacheRows, err := newCacheRows(rows) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cacheRows.clone(), nil | ||
} |
Oops, something went wrong.