shaj13 / go-guardian

Go-Guardian is a golang library that provides a simple, clean, and idiomatic way to create powerful modern API and web authentication.
MIT License
543 stars 55 forks source link

Is cache implementation thread-safe? #124

Closed amorey closed 2 years ago

amorey commented 2 years ago

I noticed that shaj13/libcache's documentation says that lru.New() initializes a non-thread safe cache (https://github.com/shaj13/libcache/blob/master/lru/lru.go#L15) and I couldn't find any code to handle thread safety in go-guardian so I'm wondering, is go-guardian thread-safe? Can it be used in the request path of a go web server safely?

shaj13 commented 2 years ago

@amorey thank you for reaching out, shaj13/libcache's and go-guardian, are operate completely independently.

go-guradian does not implement the underlying cache it only exposes an interface so the user can write their own implementation or use libcache.

However, go-guradian and libcache threaded safe and it's already running in production.

Regarding your question, all subpackage (lru, mru, etc..) initializes a non-thread safe, but the parent pkg offers a safe thread cache see example so the user can choose a safe cache or unsafe cache.

amorey commented 2 years ago

@shaj13 Thanks for your quick reply! I see, I was confused by the New() method in the lru package which returns a nonsafe cache but I see now that libcache.LRU.New() actually returns a thread-safe wrapper from the libcache package. Thanks again for help.