mirror of https://github.com/JustKato/FreePad.git
+ Cache size limit for now
This commit is contained in:
parent
0d71bf3650
commit
b447dce105
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue