Skip to content

Commit 290000d

Browse files
committed
added list
1 parent 5633e08 commit 290000d

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

cache.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ func (c *cache) set(k string, x interface{}, d time.Duration) {
8181
}
8282
}
8383

84+
func (c *cache) Append(k string, x string) error {
85+
c.mu.Lock()
86+
v, found := c.items[k]
87+
if !found || v.Expired() {
88+
c.mu.Unlock()
89+
return fmt.Errorf("Item %q not found", k)
90+
}
91+
rv, ok := v.Object.([]string)
92+
if !ok {
93+
c.mu.Unlock()
94+
return fmt.Errorf("The value for %s is not an []string", k)
95+
}
96+
nv := append(rv, x)
97+
v.Object = nv
98+
c.items[k] = v
99+
// TODO: Calls to mu.Unlock are currently not deferred because defer
100+
// adds ~200 ns (as of go1.)
101+
c.mu.Unlock()
102+
return nil
103+
}
104+
84105
// Add an item to the cache, replacing any existing item, using the default
85106
// expiration.
86107
func (c *cache) SetDefault(k string, x interface{}) {

cache_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,53 @@ func TestCache(t *testing.T) {
6868
}
6969
}
7070

71+
func TestAppend(t *testing.T) {
72+
tc := New(NoExpiration, 0)
73+
tc.Set("label1", []string{}, NoExpiration)
74+
tc.Set("label2", []string{}, NoExpiration)
75+
tc.Set("label3", []string{}, NoExpiration)
76+
77+
tc.Append("label1", "article1")
78+
tc.Append("label1", "article2")
79+
tc.Append("label2", "article1")
80+
tc.Append("label2", "article3")
81+
tc.Append("label2", "article4")
82+
tc.Append("label3", "article2")
83+
84+
label1Articles, found := tc.Get("label1")
85+
if !found {
86+
t.Error("could not find key 'label1' after appending")
87+
}
88+
label2Articles, found := tc.Get("label2")
89+
if !found {
90+
t.Error("could not find key 'label2' after appending")
91+
}
92+
label3Articles, found := tc.Get("label3")
93+
if !found {
94+
t.Error("could not find key 'label3' after appending")
95+
}
96+
97+
if l := len(label1Articles.([]string)); l != 2 {
98+
t.Errorf("'label1' should have 2 articles but has: %d", l)
99+
}
100+
if l := len(label2Articles.([]string)); l != 3 {
101+
t.Errorf("'label2' should have 3 articles but has: %d", l)
102+
}
103+
if l := len(label3Articles.([]string)); l != 1 {
104+
t.Errorf("'label3' should have 1 articles but has: %d", l)
105+
}
106+
107+
if v := label2Articles.([]string)[0]; v != "article1" {
108+
t.Errorf("first article of label2 should be article1, but is: %s", v)
109+
}
110+
if v := label2Articles.([]string)[1]; v != "article3" {
111+
t.Errorf("second article of label2 should be article3, but is: %s", v)
112+
}
113+
if v := label2Articles.([]string)[2]; v != "article4" {
114+
t.Errorf("third article of label2 should be article4, but is: %s", v)
115+
}
116+
}
117+
71118
func TestCacheTimes(t *testing.T) {
72119
var found bool
73120

0 commit comments

Comments
 (0)