Browse Source

Cache

- refactoring
Alexey Kim 1 year ago
parent
commit
b42d3f2ab5
2 changed files with 18 additions and 10 deletions
  1. 1 1
      cache/icache.go
  2. 17 9
      cache/ttl_cache.go

+ 1 - 1
cache/icache.go

@@ -6,5 +6,5 @@ type Cache interface {
 	service.Service
 
 	Get(elm Element) error
-	Set(elm Element) error
+	Set(elm ...Element)
 }

+ 17 - 9
cache/ttl_cache.go

@@ -32,17 +32,25 @@ func (c *ttlCache) Get(elm Element) error {
 	return nil
 }
 
-func (c *ttlCache) Set(elm Element) error {
-	var k = elm.Key()
-
-	buf, err := elm.Marshal()
-	if err != nil {
-		c.buf.Remove(k)
-		return err
+func (c *ttlCache) Set(items ...Element) {
+	if items == nil {
+		return
 	}
 
-	c.buf.Add(k, buf)
-	return nil
+	go func() {
+		for _, item := range items {
+			if item == nil {
+				continue
+			}
+
+			buf, err := item.Marshal()
+			if err != nil {
+				continue
+			}
+
+			c.buf.Add(item.Key(), buf)
+		}
+	}()
 }
 
 func (c *ttlCache) String() string {