Created
October 16, 2016 13:44
-
-
Save s4l1h/a313f3e2cae4126c2790b6a6ceed8fb3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main | |
| import "errors" | |
| import "fmt" | |
| var ( | |
| errorNotFound = errors.New("Cache Datası Bulunamadı: ") | |
| errorEmptyKey = errors.New("Key Boş Olamaz") | |
| ) | |
| // MemoryCache In memory'de data saklama sınıfı | |
| type MemoryCache struct { | |
| data map[string][]byte | |
| } | |
| // NewMemoryCache Yeni bir memory cache oluşturur | |
| func NewMemoryCache() *MemoryCache { | |
| return &MemoryCache{data: make(map[string][]byte)} | |
| } | |
| // Name : Cache Provider Adını döndürür | |
| func (c *MemoryCache) Name() string { | |
| return "MemoryCache" | |
| } | |
| // Get : İn Memory'de tutulan datayı alır. | |
| func (c *MemoryCache) Get(key string) ([]byte, error) { | |
| if value, oke := c.data[key]; oke == true { | |
| return value, nil | |
| } | |
| return []byte(""), errorNotFound | |
| } | |
| // Set : İn Memorye datayı kaydeder | |
| func (c *MemoryCache) Set(key string, data []byte) error { | |
| if key == "" { | |
| return errorEmptyKey | |
| } | |
| c.data[key] = data | |
| return nil | |
| } | |
| func main() { | |
| cache := NewMemoryCache() | |
| if err := cache.Set("twitter", []byte("s4l1h")); err != nil { | |
| fmt.Println("Hata Oluştu : ", err) | |
| return | |
| } | |
| value, err := cache.Get("twitter") | |
| if err != nil { | |
| fmt.Println("Hata Oluştu : ", err) | |
| return | |
| } | |
| fmt.Println(string(value)) // Çekilen Byte tipindeki datayı string'e çevirelim | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment