Skip to content

Commit

Permalink
cache pages instances with memoize
Browse files Browse the repository at this point in the history
  • Loading branch information
emad-elsaid committed May 12, 2024
1 parent 8c96311 commit 230ff46
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/emad-elsaid/xlog

go 1.22
go 1.22.3

require (
github.com/alecthomas/chroma/v2 v2.13.0
Expand All @@ -19,6 +19,14 @@ require (
golang.org/x/sync v0.6.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emad-elsaid/memoize v0.0.0-20240512184123-ac0473d98ea9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUK
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/emad-elsaid/memoize v0.0.0-20240511185021-61d3cbd8737c h1:AptXeors9nnEegXcYj7rgymKrcmNAUx3bjXoSfaY93o=
github.com/emad-elsaid/memoize v0.0.0-20240511185021-61d3cbd8737c/go.mod h1:h/SrfczQsre7AyQl2bbH6w4xLfNTQqg37uVSMXh7x9A=
github.com/emad-elsaid/memoize v0.0.0-20240512184123-ac0473d98ea9 h1:7M4FsMQup9oLgeyWi3pb0lC6oj0bDYu+S4zn5pkTxFk=
github.com/emad-elsaid/memoize v0.0.0-20240512184123-ac0473d98ea9/go.mod h1:h/SrfczQsre7AyQl2bbH6w4xLfNTQqg37uVSMXh7x9A=
github.com/emad-elsaid/types v0.0.4 h1:wk9ftd4uoRAdwDIJKzigAmexK05D0RWqNYdsYXxHZOg=
github.com/emad-elsaid/types v0.0.4/go.mod h1:7A4ii8wOJCtw6WUyJjlNVpA/AoMEfj/r8Oi2l9q5FF4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
Expand All @@ -40,11 +45,14 @@ github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kX
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.3.7/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.4.15/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down Expand Up @@ -114,3 +122,5 @@ google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGm
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9 changes: 9 additions & 0 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ func (p *page) preProcessedContent() Markdown {
func (p *page) Delete() bool {
defer Trigger(AfterDelete, p)

p.clearCache()

if p.Exists() {
err := os.Remove(p.FileName())
if err != nil {
Expand All @@ -123,6 +125,7 @@ func (p *page) Write(content Markdown) bool {
Trigger(BeforeWrite, p)
defer Trigger(AfterWrite, p)

p.clearCache()
name := p.FileName()
os.MkdirAll(filepath.Dir(name), 0700)

Expand Down Expand Up @@ -161,3 +164,9 @@ func (p *page) Emoji() string {

return ""
}

func (p *page) clearCache() {
p.content = nil
p.ast = nil
p.lastUpdate = time.Time{}
}
14 changes: 10 additions & 4 deletions page_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io/fs"
"path"
"path/filepath"

"github.com/emad-elsaid/memoize"
)

type PageSource interface {
Expand All @@ -19,7 +21,7 @@ var sources = []PageSource{
&markdownCWDFS{},
}

func NewPage(name string) (p Page) {
var NewPage = memoize.New(func(name string) (p Page) {
for i := range sources {
p = sources[i].Page(name)
if p != nil && p.Exists() {
Expand All @@ -28,7 +30,7 @@ func NewPage(name string) (p Page) {
}

return
}
})

func RegisterPageSource(p PageSource) {
sources = append([]PageSource{p}, sources...)
Expand All @@ -37,15 +39,19 @@ func RegisterPageSource(p PageSource) {
// MarkdownCWDFS a current directory markdown pages
type markdownCWDFS struct{}

// NewPage Creates an instance of Page with name. if no name is passed it's assumed INDEX
func (m *markdownCWDFS) Page(name string) Page {
var cachedPage = memoize.New(func(name string) Page {
if name == "" {
name = INDEX
}

return &page{
name: name,
}
})

// NewPage Creates an instance of Page with name. if no name is passed it's assumed INDEX
func (m *markdownCWDFS) Page(name string) Page {
return cachedPage(name)
}

func (m *markdownCWDFS) Each(ctx context.Context, f func(Page)) {
Expand Down

0 comments on commit 230ff46

Please sign in to comment.