Browse Source

Cache blocks, change code little

Vova Tkach 5 years ago
parent
commit
77df7e3b99
6 changed files with 23 additions and 29 deletions
  1. 4 5
      cblocks/block1.go
  2. 4 5
      cblocks/block2.go
  3. 4 5
      cblocks/block3.go
  4. 4 5
      cblocks/block4.go
  5. 4 5
      cblocks/block5.go
  6. 3 4
      cblocks/cblocks.go

+ 4 - 5
cblocks/block1.go

@@ -5,20 +5,19 @@ import (
 )
 
 func (this *CacheBlocks) GetBlock1(host, url string) (template.HTML, bool) {
-	this.mutex.Lock()
+	this.Lock()
+	defer this.Unlock()
 	if mapCache, ok := this.cacheBlocks[host]; ok {
 		if data, ok := mapCache.CacheBlock1[url]; ok {
-			this.mutex.Unlock()
 			return data, ok
 		}
 	}
-	this.mutex.Unlock()
 	return template.HTML(""), false
 }
 
 func (this *CacheBlocks) SetBlock1(host, url string, data template.HTML) {
-	this.mutex.Lock()
+	this.Lock()
+	defer this.Unlock()
 	this.prepare(host)
 	this.cacheBlocks[host].CacheBlock1[url] = data
-	this.mutex.Unlock()
 }

+ 4 - 5
cblocks/block2.go

@@ -5,20 +5,19 @@ import (
 )
 
 func (this *CacheBlocks) GetBlock2(host, url string) (template.HTML, bool) {
-	this.mutex.Lock()
+	this.Lock()
+	defer this.Unlock()
 	if mapCache, ok := this.cacheBlocks[host]; ok {
 		if data, ok := mapCache.CacheBlock2[url]; ok {
-			this.mutex.Unlock()
 			return data, ok
 		}
 	}
-	this.mutex.Unlock()
 	return template.HTML(""), false
 }
 
 func (this *CacheBlocks) SetBlock2(host, url string, data template.HTML) {
-	this.mutex.Lock()
+	this.Lock()
+	defer this.Unlock()
 	this.prepare(host)
 	this.cacheBlocks[host].CacheBlock2[url] = data
-	this.mutex.Unlock()
 }

+ 4 - 5
cblocks/block3.go

@@ -5,20 +5,19 @@ import (
 )
 
 func (this *CacheBlocks) GetBlock3(host, url string) (template.HTML, bool) {
-	this.mutex.Lock()
+	this.Lock()
+	defer this.Unlock()
 	if mapCache, ok := this.cacheBlocks[host]; ok {
 		if data, ok := mapCache.CacheBlock3[url]; ok {
-			this.mutex.Unlock()
 			return data, ok
 		}
 	}
-	this.mutex.Unlock()
 	return template.HTML(""), false
 }
 
 func (this *CacheBlocks) SetBlock3(host, url string, data template.HTML) {
-	this.mutex.Lock()
+	this.Lock()
+	defer this.Unlock()
 	this.prepare(host)
 	this.cacheBlocks[host].CacheBlock3[url] = data
-	this.mutex.Unlock()
 }

+ 4 - 5
cblocks/block4.go

@@ -5,20 +5,19 @@ import (
 )
 
 func (this *CacheBlocks) GetBlock4(host, url string) (template.HTML, bool) {
-	this.mutex.Lock()
+	this.Lock()
+	defer this.Unlock()
 	if mapCache, ok := this.cacheBlocks[host]; ok {
 		if data, ok := mapCache.CacheBlock4[url]; ok {
-			this.mutex.Unlock()
 			return data, ok
 		}
 	}
-	this.mutex.Unlock()
 	return template.HTML(""), false
 }
 
 func (this *CacheBlocks) SetBlock4(host, url string, data template.HTML) {
-	this.mutex.Lock()
+	this.Lock()
+	defer this.Unlock()
 	this.prepare(host)
 	this.cacheBlocks[host].CacheBlock4[url] = data
-	this.mutex.Unlock()
 }

+ 4 - 5
cblocks/block5.go

@@ -5,20 +5,19 @@ import (
 )
 
 func (this *CacheBlocks) GetBlock5(host, url string) (template.HTML, bool) {
-	this.mutex.Lock()
+	this.Lock()
+	defer this.Unlock()
 	if mapCache, ok := this.cacheBlocks[host]; ok {
 		if data, ok := mapCache.CacheBlock5[url]; ok {
-			this.mutex.Unlock()
 			return data, ok
 		}
 	}
-	this.mutex.Unlock()
 	return template.HTML(""), false
 }
 
 func (this *CacheBlocks) SetBlock5(host, url string, data template.HTML) {
-	this.mutex.Lock()
+	this.Lock()
+	defer this.Unlock()
 	this.prepare(host)
 	this.cacheBlocks[host].CacheBlock5[url] = data
-	this.mutex.Unlock()
 }

+ 3 - 4
cblocks/cblocks.go

@@ -14,13 +14,12 @@ type cache struct {
 }
 
 type CacheBlocks struct {
-	mutex       *sync.Mutex
+	sync.RWMutex
 	cacheBlocks map[string]cache
 }
 
 func New() *CacheBlocks {
 	return &CacheBlocks{
-		mutex:       &sync.Mutex{},
 		cacheBlocks: map[string]cache{},
 	}
 }
@@ -38,9 +37,9 @@ func (this *CacheBlocks) prepare(host string) {
 }
 
 func (this *CacheBlocks) Reset(host string) {
-	this.mutex.Lock()
+	this.Lock()
+	defer this.Unlock()
 	if _, ok := this.cacheBlocks[host]; ok {
 		delete(this.cacheBlocks, host)
 	}
-	this.mutex.Unlock()
 }