Browse Source

Cache blocks progress

Vova Tkach 5 years ago
parent
commit
e9b58c1aac
7 changed files with 50 additions and 26 deletions
  1. 1 3
      cblocks/block1.go
  2. 1 3
      cblocks/block2.go
  3. 1 3
      cblocks/block3.go
  4. 1 3
      cblocks/block4.go
  5. 1 3
      cblocks/block5.go
  6. 15 1
      cblocks/cblocks.go
  7. 30 10
      engine/fetdata/fetdata.go

+ 1 - 3
cblocks/block1.go

@@ -14,8 +14,6 @@ func (this *CacheBlocks) GetBlock1(host, url string) (template.HTML, bool) {
 }
 
 func (this *CacheBlocks) SetBlock1(host, url string, data template.HTML) {
-	if _, ok := this.cacheBlocks[host]; !ok {
-		this.cacheBlocks[host] = cache{}
-	}
+	this.prepare(host)
 	this.cacheBlocks[host].CacheBlock1[url] = data
 }

+ 1 - 3
cblocks/block2.go

@@ -14,8 +14,6 @@ func (this *CacheBlocks) GetBlock2(host, url string) (template.HTML, bool) {
 }
 
 func (this *CacheBlocks) SetBlock2(host, url string, data template.HTML) {
-	if _, ok := this.cacheBlocks[host]; !ok {
-		this.cacheBlocks[host] = cache{}
-	}
+	this.prepare(host)
 	this.cacheBlocks[host].CacheBlock2[url] = data
 }

+ 1 - 3
cblocks/block3.go

@@ -14,8 +14,6 @@ func (this *CacheBlocks) GetBlock3(host, url string) (template.HTML, bool) {
 }
 
 func (this *CacheBlocks) SetBlock3(host, url string, data template.HTML) {
-	if _, ok := this.cacheBlocks[host]; !ok {
-		this.cacheBlocks[host] = cache{}
-	}
+	this.prepare(host)
 	this.cacheBlocks[host].CacheBlock3[url] = data
 }

+ 1 - 3
cblocks/block4.go

@@ -14,8 +14,6 @@ func (this *CacheBlocks) GetBlock4(host, url string) (template.HTML, bool) {
 }
 
 func (this *CacheBlocks) SetBlock4(host, url string, data template.HTML) {
-	if _, ok := this.cacheBlocks[host]; !ok {
-		this.cacheBlocks[host] = cache{}
-	}
+	this.prepare(host)
 	this.cacheBlocks[host].CacheBlock4[url] = data
 }

+ 1 - 3
cblocks/block5.go

@@ -14,8 +14,6 @@ func (this *CacheBlocks) GetBlock5(host, url string) (template.HTML, bool) {
 }
 
 func (this *CacheBlocks) SetBlock5(host, url string, data template.HTML) {
-	if _, ok := this.cacheBlocks[host]; !ok {
-		this.cacheBlocks[host] = cache{}
-	}
+	this.prepare(host)
 	this.cacheBlocks[host].CacheBlock5[url] = data
 }

+ 15 - 1
cblocks/cblocks.go

@@ -17,7 +17,21 @@ type CacheBlocks struct {
 }
 
 func New() *CacheBlocks {
-	return &CacheBlocks{}
+	return &CacheBlocks{
+		cacheBlocks: map[string]cache{},
+	}
+}
+
+func (this *CacheBlocks) prepare(host string) {
+	if _, ok := this.cacheBlocks[host]; !ok {
+		this.cacheBlocks[host] = cache{
+			CacheBlock1: map[string]template.HTML{},
+			CacheBlock2: map[string]template.HTML{},
+			CacheBlock3: map[string]template.HTML{},
+			CacheBlock4: map[string]template.HTML{},
+			CacheBlock5: map[string]template.HTML{},
+		}
+	}
 }
 
 func (this *CacheBlocks) Reset(host string) {

+ 30 - 10
engine/fetdata/fetdata.go

@@ -154,26 +154,46 @@ func (this *FERData) cachedBlock(block string) template.HTML {
 }
 
 func (this *FERData) CachedBlock1() template.HTML {
-	return this.cachedBlock("cached-block-1")
-	// return template.HTML("")
+	if data, ok := this.wrap.GetBlock1(); ok {
+		return data
+	}
+	data := this.cachedBlock("cached-block-1")
+	this.wrap.SetBlock1(data)
+	return data
 }
 
 func (this *FERData) CachedBlock2() template.HTML {
-	return this.cachedBlock("cached-block-2")
-	// return template.HTML("")
+	if data, ok := this.wrap.GetBlock2(); ok {
+		return data
+	}
+	data := this.cachedBlock("cached-block-2")
+	this.wrap.SetBlock2(data)
+	return data
 }
 
 func (this *FERData) CachedBlock3() template.HTML {
-	return this.cachedBlock("cached-block-3")
-	// return template.HTML("")
+	if data, ok := this.wrap.GetBlock3(); ok {
+		return data
+	}
+	data := this.cachedBlock("cached-block-3")
+	this.wrap.SetBlock3(data)
+	return data
 }
 
 func (this *FERData) CachedBlock4() template.HTML {
-	return this.cachedBlock("cached-block-4")
-	// return template.HTML("")
+	if data, ok := this.wrap.GetBlock4(); ok {
+		return data
+	}
+	data := this.cachedBlock("cached-block-4")
+	this.wrap.SetBlock4(data)
+	return data
 }
 
 func (this *FERData) CachedBlock5() template.HTML {
-	return this.cachedBlock("cached-block-5")
-	// return template.HTML("")
+	if data, ok := this.wrap.GetBlock5(); ok {
+		return data
+	}
+	data := this.cachedBlock("cached-block-5")
+	this.wrap.SetBlock5(data)
+	return data
 }