This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gotcha): add documentations and pubic (#12)
* add docs * add docs * add docs * add LFU docs * remove TODO * update readme * update example in readme * add constructor * add default option * add test * add example to readme.md * remove whitespace
- Loading branch information
Showing
8 changed files
with
260 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,82 @@ | ||
# gotcha | ||
# gotcha | ||
|
||
gotcha: inmemory-cache in Go (Golang) with customizable algorithm | ||
|
||
[![GoDoc](https://godoc.org/github.com/bxcodec/gotcha?status.svg)](https://godoc.org/github.com/bxcodec/gotcha) | ||
|
||
## Index | ||
|
||
* [Support](#support) | ||
* [Getting Started](#getting-started) | ||
* [Example](#example) | ||
* [Contribution](#contribution) | ||
|
||
|
||
## Support | ||
|
||
You can file an [Issue](https://github.com/bxcodec/gotcha/issues/new). | ||
See documentation in [Godoc](https://godoc.org/github.com/bxcodec/gotcha) | ||
|
||
|
||
## Getting Started | ||
|
||
#### Download | ||
|
||
```shell | ||
go get -u github.com/bxcodec/gotcha | ||
``` | ||
## Example | ||
|
||
|
||
### With Cache Client | ||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/bxcodec/gotcha" | ||
) | ||
|
||
func main() { | ||
cache := gotcha.New() | ||
err := cache.Set("name", "John Snow") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
val, err := cache.Get("name") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println(val) | ||
} | ||
``` | ||
|
||
### Without Cache Client | ||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/bxcodec/gotcha" | ||
) | ||
|
||
func main() { | ||
err := gotcha.Set("name", "John Snow") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
val, err := gotcha.Get("name") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println(val) | ||
} | ||
``` | ||
|
||
|
||
## Contribution | ||
- You can submit an issue or create a Pull Request (PR) |
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,104 @@ | ||
package gotcha_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/bxcodec/gotcha" | ||
) | ||
|
||
func TestGotcha(t *testing.T) { | ||
t.Run("set", func(t *testing.T) { | ||
err := gotcha.Set("name", "John Snow") | ||
if err != nil { | ||
t.Fatalf("expected: %v, got %v", nil, err) | ||
} | ||
err = gotcha.Set("kingdom", "North Kingdom") | ||
if err != nil { | ||
t.Fatalf("expected: %v, got %v", nil, err) | ||
} | ||
}) | ||
|
||
t.Run("get", func(t *testing.T) { | ||
val, err := gotcha.Get("name") | ||
if err != nil { | ||
t.Fatalf("expected: %v, got %v", nil, err) | ||
} | ||
if val.(string) != "John Snow" { | ||
t.Fatalf("expected: %v, got %v", "John Snow", val) | ||
} | ||
}) | ||
|
||
t.Run("get-keys", func(t *testing.T) { | ||
keys, err := gotcha.GetKeys() | ||
if err != nil { | ||
t.Fatalf("expected: %v, got %v", nil, err) | ||
} | ||
var contains = func(keys []string, k string) bool { | ||
for _, item := range keys { | ||
if item == k { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
expectedKeys := []string{"name", "kingdom"} | ||
for _, k := range expectedKeys { | ||
if !contains(keys, k) { | ||
t.Fatalf("expected: %v, got: %v", true, false) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("delete", func(t *testing.T) { | ||
// Ensure the key is exists | ||
val, err := gotcha.Get("kingdom") | ||
if err != nil { | ||
t.Fatalf("expected: %v, got %v", nil, err) | ||
} | ||
if val.(string) != "North Kingdom" { | ||
t.Fatalf("expected: %v, got %v", "John Snow", val) | ||
} | ||
|
||
// Delete the Keys | ||
|
||
err = gotcha.Delete("kingdom") | ||
if err != nil { | ||
t.Fatalf("expected: %v, got %v", nil, err) | ||
} | ||
|
||
// Re-Ensure the keys is deleted | ||
val, err = gotcha.Get("kingdom") | ||
if err == nil { | ||
t.Fatalf("expected: %v, got %v", "error", err) | ||
} | ||
|
||
if val != nil { | ||
t.Fatalf("expected: %v, got %v", nil, val) | ||
} | ||
}) | ||
|
||
t.Run("clear-cache", func(t *testing.T) { | ||
// Ensure the cache is still contains item | ||
keys, err := gotcha.GetKeys() | ||
if err != nil { | ||
t.Fatalf("expected: %v, got %v", nil, err) | ||
} | ||
if len(keys) == 0 { | ||
t.Fatalf("expected: %v, got %v", "not zero", len(keys)) | ||
} | ||
|
||
err = gotcha.ClearCache() | ||
if err != nil { | ||
t.Fatalf("expected: %v, got %v", nil, err) | ||
} | ||
|
||
// Re-Ensure the cache already cleared | ||
keys, err = gotcha.GetKeys() | ||
if err != nil { | ||
t.Fatalf("expected: %v, got %v", nil, err) | ||
} | ||
if len(keys) != 0 { | ||
t.Fatalf("expected: %v, got %v", "zero", len(keys)) | ||
} | ||
}) | ||
} |
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,7 @@ | ||
# LFU Algorithm | ||
|
||
This LFU Algorithm implemented based on this paper: "[An O(1) algorithm for implementing the LFU | ||
cache eviction scheme](http://dhruvbird.com/lfu.pdf)" | ||
(by Prof. Ketan Shah, Anirban Mitra, and Dhruv Matani) | ||
|
||
Well, to be honest, it's not really exaclty as is like they wrote in the pseudocode. Because I need to change a few flow of the code due to the lack of Golang itself. |
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