+ Cache size limit for now

This commit is contained in:
Daniel Legt 2022-05-15 16:12:28 +03:00
parent 0d71bf3650
commit b447dce105
2 changed files with 43 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package post
import ( import (
"errors" "errors"
"github.com/JustKato/FreePad/helper"
"github.com/JustKato/FreePad/models/database" "github.com/JustKato/FreePad/models/database"
) )
@ -14,6 +15,23 @@ func GetPostList() []*Post {
return postList 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) { func Create(name string, content string) (*Post, error) {
if len(name) < 1 { if len(name) < 1 {
@ -34,8 +52,11 @@ func Create(name string, content string) (*Post, error) {
Content: content, Content: content,
} }
// Check if we can cache this element
if len(postMap) < helper.GetCacheMapLimit() {
// Set the post by name // Set the post by name
postMap[name] = myPost postMap[name] = myPost
}
// Add the post to the database // Add the post to the database
db, err := database.GetConn() db, err := database.GetConn()

View File

@ -1,6 +1,9 @@
package helper package helper
import "os" import (
"os"
"strconv"
)
func GetDomainBase() string { func GetDomainBase() string {
domainBase, domainExists := os.LookupEnv("DOMAIN_BASE") domainBase, domainExists := os.LookupEnv("DOMAIN_BASE")
@ -12,3 +15,19 @@ func GetDomainBase() string {
return domainBase 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
}