') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); GitHub - byte0o/multilayer_cache: Multi-layer caching implemented by golang · GitHub
Skip to content

Latest commit

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Multi-layer caching implemented by golang

example

local caeche

package cache
import (
"context""time"
)
typeLocalCache[Tany] struct {
namestringcachemap[string]TzeroT
}
var_Cache[any] = (*LocalCache[any])(nil)
// NewLocalCache// @name cache name // @defaultExpiration default cache key expiration time// @cleanupInterval default time between cache cleanupsfuncNewLocalCache[Tany](namestring, defaultExpiration, cleanupInterval time.Duration) Cache[T] {
return&LocalCache[T]{
name: name,
cache: make(map[string]T),
}
}
func (lc*LocalCache[T]) Get(_ context.Context, keystring) (T, bool, error) {
v, exist:=lc.cache[key]
if!exist {
returnlc.zero, false, nil
}
vv, _:=v.(T)
returnvv, exist, nil
}
func (lc*LocalCache[T]) Set(_ context.Context, keystring, tT, _ time.Duration) error {
lc.cache[key] =treturnnil
}
func (lc*LocalCache[T]) Delete(_ context.Context, keys...string) error {
for_, key:=rangekeys {
delete(lc.cache, key)
}
returnnil
}
func (lc*LocalCache[T]) Name() string {
returnlc.name
}

remote cache

package cache
import (
"context""fmt""github.com/redis/go-redis/v9""reflect""time"
)
typeRedisClientstruct {
*redis.Client
}
funcNewRedisClient(ctx context.Context, options*redis.Options) (*RedisClient, error) {
rc:=&RedisClient{redis.NewClient(options)}
returnrc, rc.Ping(ctx).Err()
}
func (rc*RedisClient) Close() error {
returnrc.Client.Close()
}
// RedisCache// Redis Cache the implementation only supports the normal key/value formattypeRedisCache[Tany] struct {
namestringcache*RedisClientzeroT
}
funcNewRedisCache[Tany](namestring, rc*RedisClient) Cache[T] {
return&RedisCache[T]{
name: name,
cache: rc,
}
}
func (rc*RedisCache[T]) Name() string {
returnrc.name
}
func (rc*RedisCache[T]) Get(ctx context.Context, keystring) (T, bool, error) {
strCmd:=rc.cache.Get(ctx, key)
value, err:=strCmd.Result()
iferr!=nil {
iferr==redis.Nil {
returnrc.zero, false, nil
}
returnrc.zero, false, err
}
varvTswitchreflect.TypeOf(rc.zero).Kind() {
casereflect.String:
varptranyptr=valuev=ptr.(T)
default:
err=json.Unmarshal([]byte(value), &v)
iferr!=nil {
returnrc.zero, false, err
}
}
returnv, true, err
}
func (rc*RedisCache[T]) Set(ctx context.Context, keystring, tT, expiration time.Duration) error {
varvstringswitchreflect.TypeOf(t).Kind() {
casereflect.String:
v=fmt.Sprintf("%v", t)
default:
body, err:=json.Marshal(t)
iferr!=nil {
returnerr
}
v=string(body)
}
statusCmd:=rc.cache.Set(ctx, key, v, expiration)
returnstatusCmd.Err()
}
func (rc*RedisCache[T]) Delete(ctx context.Context, keys...string) error {
intCmd:=rc.cache.Del(ctx, keys...)
returnintCmd.Err()
}
func (rc*RedisCache[T]) Close() error {
returnrc.cache.Close()
}

use

ctx:=context.Background()
local:=NewLocalCache[string]("local", 1*time.Minute, 1*time.Minute)
rc, err:=NewRedisClient(ctx, &redis.Options{
Addr: "",
Password: "",
})
iferr!=nil{
panic(err)
}
remote:=NewRedisCache[string]("remote", rc)
mCache:=NewMultilayerCache[string](1*time.Minute, local, remote)
defermCache.Close()
err=mCache.Set(ctx,"key","value")
iferr!=nil{
panic(err)
}
fmt.Println(mCache.Get(ctx,"key"))

About

Multi-layer caching implemented by golang

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages