قم بتسريع مشاريع Golang الخاصة بك دون الحاجة إلى إنشاء قاعدة بيانات في كل مرة تبدأ فيها مشروعًا جديدًا. هل سئمت من تكوين قواعد البيانات من الصفر؟ فقط لمواجهة مشاكل جديدة؟ لا مزيد من البحث في هذه المدونة، سنبحث في مكتبة التخزين المؤقت لـ Golang مع دعم TTL، مثابرة القرص و نوع بيانات التجزئة.
جو سويفت.
import ( "fmt" "github.com/leoantony72/goswift" ) func main(){ cache := goswift.NewCache() // Value 0 indicates no expiry cache.Set("key", "value", 0) val, err := cache.Get("key") if err !=nil{ fmt.Println(err) return } fmt.Println("key", val) }
// Update value // @Update(key string, val interface{}) error err = cache.Update("key","value2") if err != nil{ fmt.Println(err) return }
// Delete command // @Del(key string) cache.Del("key") // Exist command // @Exists(key string) bool value = cache.Exists("key") fmt.Println(value) // returns false
// Set Value with Expiry // @Set(key string, val interface{}, exp int) // Here expiry is set to 1sec cache.Set("key","value",1000) // Hset command // @Hset(key, field string, value interface{}, exp int) // in this case the "key" expires in 1sec cache.Hset("key","name","value",1000) cache.Hset("key","age",18,1000)
// Hset command // @Hset(key, field string, value interface{}, exp int) // in this case the "key" expires in 1sec cache.Hset("key","name","value",1000) cache.Hset("key","age",18,1000) // HMset command // @HMset(key string, d interface{}, exp int) error // Set a Hash by passing a Struct/Map // ---by passing a struct--- type Person struct{ Name string Age int Place string } person1 := &Person{Name:"bob",Age:18,Place:"NYC"} err = cache.HMset("key",person1) if err != nil{ fmt.Println(err) return } // ---by passing a map--- person2 := map[string]interface{Name:"john",Age:18,Place:"NYC"} err = cache.HMset("key",person2) if err != nil{ fmt.Println(err) return } // Hget command // @HGet(key, field string) (interface{}, error) // get individual fields in Hash data,err := cache.HGet("key","field") if err != nil{ fmt.Println(err) return } fmt.Println(data) // HgetAll command // @HGetAll(key string) (map[string]interface{}, error) // gets all the fields with value in a hash key // retuns a map[string]interface{} data,err = cache.HGetAll("key") if err != nil{ fmt.Println(err) return }
opt := goswift.CacheOptions{ EnableSnapshots: true, SnapshotInterval: time.Second*5, } c := goswift.NewCache(opt)
سيؤدي هذا إلى أخذ لقطة من البيانات كل 5 ثوانٍ وحفظها في ملف Snapshot.data. بشكل افتراضي، يتم تعطيل اللقطات وإذا لم يتم توفير SnapshotInterval، فإن القيمة الافتراضية هي 5 ثوانٍ.
ملاحظة: إذا كانت EnableSnapshot خاطئة، فلن يتم استيراد البيانات المحفوظة في الملف
const ( ErrKeyNotFound = "key does not Exists" ErrFieldNotFound = "field does not Exists" ErrNotHashvalue = "not a Hash value/table" ErrHmsetDataType = "invalid data type, Expected Struct/Map" )
هذه هي الأخطاء الشائعة التي قد تحدث أثناء كتابة الكود. توفر لك هذه المتغيرات طريقة واضحة وسهلة لمقارنة الأخطاء لتحديد الأخطاء.
data,err := cache.Get("key") if err != nil { if err.Error() == goswift.ErrKeyNotFound { //do something } }
كل 3 ثوانٍ، يتم استدعاء وظيفة **sweaper ** لمسح القيم منتهية الصلاحية من جدول التجزئة. نحتفظ بكومة دقيقة تشير إلى خريطة التجزئة. سيكون العنصر العلوي هو المفتاح ذو أصغر TTL. نجتاز الشجرة حتى تصبح مدة البقاء (TTL) أكبر من الوقت الحالي.
لا أنصحك باستخدام هذا في الإنتاج!!، لكن لا تتردد في استخدامه في مشروعك الجانبي الصغير. قم بتجربته وإذا واجهت خطأ ما، فقم بإجراء مشكلة في GitHub repo.
البريد الإلكتروني: [email protected]
جيثب: https://github.com/leoantony72
الريبو: https://github.com/leoantony72/goswift
تنصل: جميع الموارد المقدمة هي جزئيًا من الإنترنت. إذا كان هناك أي انتهاك لحقوق الطبع والنشر الخاصة بك أو الحقوق والمصالح الأخرى، فيرجى توضيح الأسباب التفصيلية وتقديم دليل على حقوق الطبع والنشر أو الحقوق والمصالح ثم إرسالها إلى البريد الإلكتروني: [email protected]. سوف نتعامل مع الأمر لك في أقرب وقت ممكن.
Copyright© 2022 湘ICP备2022001581号-3