-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api structure simplified (breaking changes), project directory struct…
…ure refactored
- Loading branch information
1 parent
1ca0d8f
commit 5377ed5
Showing
53 changed files
with
1,333 additions
and
1,531 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,6 +1,7 @@ | ||
# Project | ||
.idea | ||
vendor | ||
mnemosyne-inpkg | ||
_vendor-* | ||
.tmp | ||
bin | ||
|
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 was deleted.
Oops, something went wrong.
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,99 @@ | ||
package cache | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/piotrkowalczuk/mnemosyne/mnemosynerpc" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
// DefaultSize determines how big cache should be at the beginning. | ||
const DefaultSize = 100000 | ||
|
||
type Entry struct { | ||
Ses mnemosynerpc.Session | ||
Exp time.Time | ||
Refresh bool | ||
} | ||
|
||
type Cache struct { | ||
data map[uint64]*Entry | ||
dataLock sync.RWMutex | ||
TTL time.Duration | ||
// monitoring | ||
hitsTotal prometheus.Counter | ||
missesTotal prometheus.Counter | ||
refreshTotal prometheus.Counter | ||
} | ||
|
||
func New(ttl time.Duration, namespace string) *Cache { | ||
return &Cache{ | ||
TTL: ttl, | ||
data: make(map[uint64]*Entry, DefaultSize), | ||
hitsTotal: prometheus.NewCounter(prometheus.CounterOpts{ | ||
Namespace: namespace, | ||
Subsystem: "cache", | ||
Name: "hits_total", | ||
Help: "Total number of cache hits.", | ||
}), | ||
missesTotal: prometheus.NewCounter(prometheus.CounterOpts{ | ||
Namespace: namespace, | ||
Subsystem: "cache", | ||
Name: "misses_total", | ||
Help: "Total number of cache misses.", | ||
}), | ||
refreshTotal: prometheus.NewCounter(prometheus.CounterOpts{ | ||
Namespace: namespace, | ||
Subsystem: "cache", | ||
Name: "refresh_total", | ||
Help: "Total number of times cache Refresh.", | ||
}), | ||
} | ||
} | ||
|
||
func (c *Cache) Refresh(k uint64) { | ||
c.refreshTotal.Add(1) | ||
|
||
c.dataLock.Lock() | ||
c.data[k].Refresh = true | ||
c.dataLock.Unlock() | ||
} | ||
|
||
func (c *Cache) Put(k uint64, ses mnemosynerpc.Session) { | ||
c.dataLock.Lock() | ||
c.data[k] = &Entry{Ses: ses, Exp: time.Now().Add(c.TTL), Refresh: false} | ||
c.dataLock.Unlock() | ||
} | ||
|
||
func (c *Cache) Del(k uint64) { | ||
c.dataLock.Lock() | ||
delete(c.data, k) | ||
c.dataLock.Unlock() | ||
} | ||
|
||
func (c *Cache) Read(k uint64) (*Entry, bool) { | ||
c.dataLock.RLock() | ||
entry, ok := c.data[k] | ||
c.dataLock.RUnlock() | ||
if ok { | ||
c.hitsTotal.Add(1) | ||
} else { | ||
c.missesTotal.Add(1) | ||
} | ||
return entry, ok | ||
} | ||
|
||
// Collect implements prometheus Collector interface. | ||
func (c *Cache) Collect(in chan<- prometheus.Metric) { | ||
c.hitsTotal.Collect(in) | ||
c.refreshTotal.Collect(in) | ||
c.missesTotal.Collect(in) | ||
} | ||
|
||
// Describe implements prometheus Collector interface. | ||
func (c *Cache) Describe(in chan<- *prometheus.Desc) { | ||
c.hitsTotal.Describe(in) | ||
c.refreshTotal.Describe(in) | ||
c.missesTotal.Describe(in) | ||
} |
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,46 @@ | ||
package model_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/piotrkowalczuk/mnemosyne/internal/model" | ||
) | ||
|
||
func TestBackpack(t *testing.T) { | ||
b := model.Bag{} | ||
|
||
b.Set("key", "value") | ||
|
||
if !b.Has("key") { | ||
t.Errorf("bagpack should have specified key") | ||
} | ||
|
||
if b.Get("key") != "value" { | ||
t.Errorf("bagpack should have specified key") | ||
} | ||
} | ||
|
||
func TestBackpack_Scan(t *testing.T) { | ||
b := model.Bag{"A": "B"} | ||
val, err := b.Value() | ||
if err != nil { | ||
t.Fatalf("unexpected error: %s", err.Error()) | ||
} | ||
b.Set("A", "C") | ||
if err = b.Scan(val); err != nil { | ||
t.Fatalf("unexpected error: %s", err.Error()) | ||
} | ||
if b.Get("A") != "B" { | ||
t.Errorf("wrong output, got %s", b.Get("A")) | ||
} | ||
|
||
exp := "unsupported data source type" | ||
b = model.Bag{} | ||
if err := b.Scan("data"); err != nil { | ||
if err.Error() != exp { | ||
t.Errorf("wrong error, expected %s but got %s", exp, err.Error()) | ||
} | ||
} else { | ||
t.Error("error expected, got nil") | ||
} | ||
} |
Oops, something went wrong.