Browse Source

Cache blocks progress

Vova Tkach 5 years ago
parent
commit
2cd661e5ff
4 changed files with 57 additions and 4 deletions
  1. 33 0
      cblocks/cblocks.go
  2. 3 2
      engine/engine.go
  3. 16 1
      engine/wrapper/wrapper.go
  4. 5 1
      main.go

+ 33 - 0
cblocks/cblocks.go

@@ -0,0 +1,33 @@
+package cblocks
+
+import (
+	"html/template"
+)
+
+type cache struct {
+	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
+}
+
+type CacheBlocks struct {
+	cacheBlocks map[string]cache
+}
+
+func New() *CacheBlocks {
+	return &CacheBlocks{}
+}
+
+func (this *CacheBlocks) ResetBlock1(host string) {
+	//
+}
+
+func (this *CacheBlocks) GetBlock1(host, url string) (template.HTML, bool) {
+	return template.HTML(""), false
+}
+
+func (this *CacheBlocks) SetBlock1(host, url string, data template.HTML) {
+	//
+}

+ 3 - 2
engine/engine.go

@@ -6,6 +6,7 @@ import (
 	"strings"
 
 	"golang-fave/assets"
+	"golang-fave/cblocks"
 	"golang-fave/engine/mysqlpool"
 	"golang-fave/engine/wrapper"
 	"golang-fave/logger"
@@ -20,8 +21,8 @@ type Engine struct {
 	Mods *modules.Modules
 }
 
-func Response(mp *mysqlpool.MySqlPool, l *logger.Logger, m *modules.Modules, w http.ResponseWriter, r *http.Request, s *session.Session, host, port, chost, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp string) bool {
-	wrap := wrapper.New(l, w, r, s, host, port, chost, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp, mp)
+func Response(mp *mysqlpool.MySqlPool, l *logger.Logger, m *modules.Modules, w http.ResponseWriter, r *http.Request, s *session.Session, c *cblocks.CacheBlocks, host, port, chost, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp string) bool {
+	wrap := wrapper.New(l, w, r, s, c, host, port, chost, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp, mp)
 	eng := &Engine{
 		Wrap: wrap,
 		Mods: m,

+ 16 - 1
engine/wrapper/wrapper.go

@@ -12,6 +12,7 @@ import (
 	"strings"
 	"time"
 
+	"golang-fave/cblocks"
 	"golang-fave/consts"
 	"golang-fave/engine/mysqlpool"
 	"golang-fave/engine/sqlw"
@@ -30,6 +31,7 @@ type Wrapper struct {
 	W http.ResponseWriter
 	R *http.Request
 	S *session.Session
+	c *cblocks.CacheBlocks
 
 	Host     string
 	Port     string
@@ -53,7 +55,7 @@ type Wrapper struct {
 	User *utils.MySql_user
 }
 
-func New(l *logger.Logger, w http.ResponseWriter, r *http.Request, s *session.Session, host, port, chost, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp string, mp *mysqlpool.MySqlPool) *Wrapper {
+func New(l *logger.Logger, w http.ResponseWriter, r *http.Request, s *session.Session, c *cblocks.CacheBlocks, host, port, chost, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp string, mp *mysqlpool.MySqlPool) *Wrapper {
 
 	conf := configNew()
 	if err := conf.configRead(dirConfig + string(os.PathSeparator) + "config.json"); err != nil {
@@ -65,6 +67,7 @@ func New(l *logger.Logger, w http.ResponseWriter, r *http.Request, s *session.Se
 		W:             w,
 		R:             r,
 		S:             s,
+		c:             c,
 		Host:          host,
 		Port:          port,
 		CurrHost:      chost,
@@ -270,6 +273,18 @@ func (this *Wrapper) ConfigSave() error {
 	return this.Config.configWrite(this.DConfig + string(os.PathSeparator) + "config.json")
 }
 
+func (this *Wrapper) ResetBlock1() {
+	this.c.ResetBlock1(this.Host)
+}
+
+func (this *Wrapper) GetBlock1() (template.HTML, bool) {
+	return this.c.GetBlock1(this.Host, this.R.URL.Path)
+}
+
+func (this *Wrapper) SetBlock1(data template.HTML) {
+	this.c.SetBlock1(this.Host, this.R.URL.Path, data)
+}
+
 func (this *Wrapper) RemoveProductImageThumbnails(product_id, filename string) error {
 	pattern := this.DHtdocs + string(os.PathSeparator) + strings.Join([]string{"products", "images", product_id, filename}, string(os.PathSeparator))
 	if files, err := filepath.Glob(pattern); err != nil {

+ 5 - 1
main.go

@@ -8,6 +8,7 @@ import (
 	"os"
 
 	"golang-fave/assets"
+	"golang-fave/cblocks"
 	"golang-fave/consts"
 	"golang-fave/domains"
 	"golang-fave/engine"
@@ -97,6 +98,9 @@ func main() {
 	// MySQL connections pool
 	mpool := mysqlpool.New()
 
+	// Init cache blocks
+	cbs := cblocks.New()
+
 	// Init and start web server
 	bootstrap.Start(lg.Handler, fmt.Sprintf("%s:%d", consts.ParamHost, consts.ParamPort), 9, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request, o interface{}) {
 		w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
@@ -185,7 +189,7 @@ func main() {
 
 		// Logic
 		if mp != nil {
-			if engine.Response(mp, lg, mods, w, r, sess, host, port, curr_host, vhost_dir_config, vhost_dir_htdocs, vhost_dir_logs, vhost_dir_template, vhost_dir_tmp) {
+			if engine.Response(mp, lg, mods, w, r, sess, cbs, host, port, curr_host, vhost_dir_config, vhost_dir_htdocs, vhost_dir_logs, vhost_dir_template, vhost_dir_tmp) {
 				return
 			}
 		}