From b447dce1053044ade86e80cc821db8e21401c2fa Mon Sep 17 00:00:00 2001 From: Kato Twofold Date: Sun, 15 May 2022 16:12:28 +0300 Subject: [PATCH] + Cache size limit for now --- src/controllers/post/post.go | 25 +++++++++++++++++++++++-- src/helper/helper_main.go | 21 ++++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/controllers/post/post.go b/src/controllers/post/post.go index 5408ae2..7e757e9 100644 --- a/src/controllers/post/post.go +++ b/src/controllers/post/post.go @@ -3,6 +3,7 @@ package post import ( "errors" + "github.com/JustKato/FreePad/helper" "github.com/JustKato/FreePad/models/database" ) @@ -14,6 +15,23 @@ func GetPostList() []*Post { return postList } +func Retrieve(name string) (*Post, error) { + + if len(name) < 1 { + return nil, errors.New("the name of the post must contain at least 1 character") + } + + if len(name) > 256 { + return nil, errors.New("the name of the post must not exceed 256 characters") + } + + // Check if we have the post cached + if val, ok := postMap[name]; ok { + return &val, nil + } + +} + func Create(name string, content string) (*Post, error) { if len(name) < 1 { @@ -34,8 +52,11 @@ func Create(name string, content string) (*Post, error) { Content: content, } - // Set the post by name - postMap[name] = myPost + // Check if we can cache this element + if len(postMap) < helper.GetCacheMapLimit() { + // Set the post by name + postMap[name] = myPost + } // Add the post to the database db, err := database.GetConn() diff --git a/src/helper/helper_main.go b/src/helper/helper_main.go index 54e620d..bb82982 100644 --- a/src/helper/helper_main.go +++ b/src/helper/helper_main.go @@ -1,6 +1,9 @@ package helper -import "os" +import ( + "os" + "strconv" +) func GetDomainBase() string { domainBase, domainExists := os.LookupEnv("DOMAIN_BASE") @@ -12,3 +15,19 @@ func GetDomainBase() string { return domainBase } + +func GetCacheMapLimit() int { + cacheMapLimit, domainExists := os.LookupEnv("CACHE_MAP_LIMIT") + + if !domainExists { + os.Setenv("CACHE_MAP_LIMIT", "25") + cacheMapLimit = "25" + } + + rez, err := strconv.Atoi(cacheMapLimit) + if err != nil { + return 25 + } + + return rez +}