Skip to content

Cache static files in memory#15

Open
toshinari123 wants to merge 24 commits into
oursky:mainfrom
toshinari123:serve-cache-compressed-content
Open

Cache static files in memory#15
toshinari123 wants to merge 24 commits into
oursky:mainfrom
toshinari123:serve-cache-compressed-content

Conversation

@toshinari123

Copy link
Copy Markdown
Contributor

No description provided.

@toshinari123 toshinari123 requested a review from kiootic January 10, 2024 09:51
@toshinari123 toshinari123 marked this pull request as draft January 10, 2024 09:57
Comment thread internal/handler/site/site_handler.go Outdated
Comment on lines +96 to +97
writer := httputil.NewTimeoutResponseWriter(w, 10*time.Second)
http.ServeContent(writer, r, path.Base(r.URL.Path), info.ModTime, cacheReader)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is duplicated, please refactor.

Comment thread internal/cache/contentcache.go Outdated
return &ContentCache{m: m, cache: cache}, nil
}

func (c *ContentCache) GetContent(id string, r io.Reader) (*bytes.Buffer, error) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This signature is inappropiate, caches should return what is put in.

Comment thread internal/cache/contentcache.go Outdated
func NewContentCache() (*ContentCache, error) {
m := make(map[string]*sync.Mutex)
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this number derived?

Comment thread internal/cache/contentcache.go Outdated
m := make(map[string]*sync.Mutex)
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,
MaxCost: contentCacheSize,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be passed in as argument.

Comment thread internal/cache/contentcache.go Outdated
BufferItems: 64,
OnExit: func(item interface{}) {
cell := item.(contentCacheCell)
delete(m, cell.hash)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Access to this map should be synchronized.

@kiootic

kiootic commented Jan 24, 2024

Copy link
Copy Markdown
Collaborator

Please write a test for the ContentCache:

@kiootic kiootic changed the title draft: Serve cache compressed content Cache static files in memory Jan 24, 2024
@kiootic kiootic marked this pull request as ready for review January 30, 2024 04:01
Instead of storing key and value as value, use a reference-counted map of rwmutexes (it automatically deletes mutexes so no need onExit in Ristretto)

Separated into getContent and setContent in order to allow getContent call without io.ReadSeeker

Used generics
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants