Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache static files in memory #15

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

toshinari123
Copy link
Contributor

No description provided.

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
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.

return &ContentCache{m: m, cache: cache}, nil
}

func (c *ContentCache) GetContent(id string, r io.Reader) (*bytes.Buffer, error) {
Copy link
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.

func NewContentCache() (*ContentCache, error) {
m := make(map[string]*sync.Mutex)
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,
Copy link
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?

m := make(map[string]*sync.Mutex)
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7,
MaxCost: contentCacheSize,
Copy link
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.

BufferItems: 64,
OnExit: func(item interface{}) {
cell := item.(contentCacheCell)
delete(m, cell.hash)
Copy link
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
Copy link
Collaborator

kiootic commented Jan 24, 2024

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