سريع ومتزامن ، يفسد ذاكرة التخزين المؤقت في الذاكرة المكتوبة للحفاظ على عدد كبير من الإدخالات دون تأثير على الأداء. BigCache يحافظ على إدخالات على الكومة ولكن يحذف GC لهم. ولتحقيق ذلك ، تحدث عمليات على شرائح البايت ، وبالتالي ستكون هناك حاجة إلى إدخالات (DE) أمام ذاكرة التخزين المؤقت في معظم حالات الاستخدام.
يتطلب الذهاب 1.12 أو أحدث.
import (
"fmt"
"context"
"github.com/allegro/bigcache/v3"
)
cache , _ := bigcache . New ( context . Background (), bigcache . DefaultConfig ( 10 * time . Minute ))
cache . Set ( "my-unique-key" , [] byte ( "value" ))
entry , _ := cache . Get ( "my-unique-key" )
fmt . Println ( string ( entry ))عندما يمكن التنبؤ بحمل ذاكرة التخزين المؤقت مسبقًا ، فمن الأفضل استخدام التهيئة المخصصة لأنه يمكن تجنب تخصيص الذاكرة الإضافي بهذه الطريقة.
import (
"log"
"github.com/allegro/bigcache/v3"
)
config := bigcache. Config {
// number of shards (must be a power of 2)
Shards : 1024 ,
// time after which entry can be evicted
LifeWindow : 10 * time . Minute ,
// Interval between removing expired entries (clean up).
// If set to <= 0 then no action is performed.
// Setting to < 1 second is counterproductive — bigcache has a one second resolution.
CleanWindow : 5 * time . Minute ,
// rps * lifeWindow, used only in initial memory allocation
MaxEntriesInWindow : 1000 * 10 * 60 ,
// max entry size in bytes, used only in initial memory allocation
MaxEntrySize : 500 ,
// prints information about additional memory allocation
Verbose : true ,
// cache will not allocate more memory than this limit, value in MB
// if value is reached then the oldest entries can be overridden for the new ones
// 0 value means no size limit
HardMaxCacheSize : 8192 ,
// callback fired when the oldest entry is removed because of its expiration time or no space left
// for the new entry, or because delete was called. A bitmask representing the reason will be returned.
// Default value is nil which means no callback and it prevents from unwrapping the oldest entry.
OnRemove : nil ,
// OnRemoveWithReason is a callback fired when the oldest entry is removed because of its expiration time or no space left
// for the new entry, or because delete was called. A constant representing the reason will be passed through.
// Default value is nil which means no callback and it prevents from unwrapping the oldest entry.
// Ignored if OnRemove is specified.
OnRemoveWithReason : nil ,
}
cache , initErr := bigcache . New ( context . Background (), config )
if initErr != nil {
log . Fatal ( initErr )
}
cache . Set ( "my-unique-key" , [] byte ( "value" ))
if entry , err := cache . Get ( "my-unique-key" ); err == nil {
fmt . Println ( string ( entry ))
}LifeWindow و CleanWindow LifeWindow هو وقت. بعد ذلك الوقت ، يمكن استدعاء إدخال ميت ولكن لم يتم حذفه.
CleanWindow هو الوقت. بعد ذلك الوقت ، سيتم حذف جميع الإدخالات الميتة ، ولكن ليس الإدخالات التي لا تزال تتمتع بالحياة.
وتمت مقارنة ثلاثة ذاكرة التخزين المؤقت: Bigcache و Freecache و Map. تم إجراء الاختبارات القياسية باستخدام وحدة المعالجة المركزية I7-6700K @ 4.00 جيجا هرتز مع 32 جيجابايت من ذاكرة الوصول العشوائي على Ubuntu 18.04 LTS (5.2.12-050212-Generic).
يمكن العثور على رمز المصدر المعايير هنا
go version
go version go1.13 linux/amd64
go test -bench=. -benchmem -benchtime=4s ./... -timeout 30m
goos: linux
goarch: amd64
pkg: github.com/allegro/bigcache/v3/caches_bench
BenchmarkMapSet-8 12999889 376 ns/op 199 B/op 3 allocs/op
BenchmarkConcurrentMapSet-8 4355726 1275 ns/op 337 B/op 8 allocs/op
BenchmarkFreeCacheSet-8 11068976 703 ns/op 328 B/op 2 allocs/op
BenchmarkBigCacheSet-8 10183717 478 ns/op 304 B/op 2 allocs/op
BenchmarkMapGet-8 16536015 324 ns/op 23 B/op 1 allocs/op
BenchmarkConcurrentMapGet-8 13165708 401 ns/op 24 B/op 2 allocs/op
BenchmarkFreeCacheGet-8 10137682 690 ns/op 136 B/op 2 allocs/op
BenchmarkBigCacheGet-8 11423854 450 ns/op 152 B/op 4 allocs/op
BenchmarkBigCacheSetParallel-8 34233472 148 ns/op 317 B/op 3 allocs/op
BenchmarkFreeCacheSetParallel-8 34222654 268 ns/op 350 B/op 3 allocs/op
BenchmarkConcurrentMapSetParallel-8 19635688 240 ns/op 200 B/op 6 allocs/op
BenchmarkBigCacheGetParallel-8 60547064 86.1 ns/op 152 B/op 4 allocs/op
BenchmarkFreeCacheGetParallel-8 50701280 147 ns/op 136 B/op 3 allocs/op
BenchmarkConcurrentMapGetParallel-8 27353288 175 ns/op 24 B/op 2 allocs/op
PASS
ok github.com/allegro/bigcache/v3/caches_bench 256.257sيكتب ويقرأ في BigCache أسرع من Freecache. يكتب إلى الخريطة هي أبطأ.
go version
go version go1.13 linux/amd64
go run caches_gc_overhead_comparison.go
Number of entries: 20000000
GC pause for bigcache: 1.506077ms
GC pause for freecache: 5.594416ms
GC pause for map: 9.347015ms go version
go version go1.13 linux/arm64
go run caches_gc_overhead_comparison.go
Number of entries: 20000000
GC pause for bigcache: 22.382827ms
GC pause for freecache: 41.264651ms
GC pause for map: 72.236853ms
يوضح الاختبار مدى قلة الإيقاف المؤقتة للـ GC للذاكرة التخزين المؤقت مع 20 مل من الإدخالات. BigCache و Freecache لديهم وقت الإيقاف المؤقت GC متشابهة للغاية.
قد تواجه الذاكرة في النظام الذي يبدو أنه زيادة كبيرة ، ولكن هذا هو السلوك المتوقع. Go Runtime يخصص الذاكرة في أجزاء أو "تمتد" وسيبلغ نظام التشغيل عندما لم يعد مطلوبًا عن طريق تغيير حالتهم إلى "الخمول". ستبقى "الامتدادات" جزءًا من استخدام موارد العملية حتى يحتاج نظام التشغيل إلى إعادة عرض العنوان. مزيد من القراءة المتاحة هنا.
يعتمد BigCache على التحسين المقدم في إصدار 1.5 من GO (العدد-9477). ينص هذا التحسين على أنه إذا تم استخدام خريطة بدون مؤشرات في المفاتيح والقيم ، فسوف يحذف GC محتواه. لذلك يستخدم BigCache map[uint64]uint32 حيث يتم تجزئة المفاتيح والقيم هي إزاحة من الإدخالات.
يتم الاحتفاظ بالإدخالات في شرائح البايت ، لحذف GC مرة أخرى. يمكن أن ينمو حجم شرائح البايت إلى gigabytes دون تأثير على الأداء لأن GC لن ترى مؤشرًا واحدًا إلا.
BigCache لا يتعامل مع الاصطدامات. عند إدراج عنصر جديد ويصطدم تجزئة بالعنصر المخزن مسبقًا ، فإن العنصر الجديد يرتفع القيمة التي تم تخزينها مسبقًا.
يوفر كلا ذاكرة التخزين المؤقت نفس الميزات الأساسية ولكنها تقلل من النفقات العامة GC بطرق مختلفة. يعتمد BigCache على map[uint64]uint32 ، يقوم Freecache بتنفيذ رسم الخرائط الخاص به على شرائح لتقليل عدد المؤشرات.
وترد نتائج الاختبارات القياسية أعلاه. تتمثل أحد ميزة BigCache على Freecache في أنك لست بحاجة إلى معرفة حجم ذاكرة التخزين المؤقت مقدمًا ، لأنه عندما يكون BigCache ممتلئًا ، يمكنه تخصيص ذاكرة إضافية للإدخالات الجديدة بدلاً من الكتابة فوقها الحالية كما يفعل Freecache حاليًا. ومع ذلك ، يمكن أيضًا ضبط حجم الأقصى الصلب في BigCache ، تحقق من HardMaxCachesize.
تتضمن هذه الحزمة أيضًا تطبيق HTTP القابل للنشر بسهولة لـ BigCache ، والذي يمكن العثور عليه في حزمة الخادم.
تم وصف BigCache Genesis في منشور مدونة Allegro.tech: كتابة خدمة ذاكرة التخزين المؤقت السريعة للغاية في GO
يتم إصدار BigCache بموجب ترخيص Apache 2.0 (انظر الترخيص)