patrickmn / go-cache

An in-memory key:value store/cache (similar to Memcached) library for Go, suitable for single-machine applications.
https://patrickmn.com/projects/go-cache/
MIT License
8.16k stars 874 forks source link

Set function takes 900ms+ when storing sessions #133

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hello,

I'm trying to use go-cache for session storage, I am storing hashed session tokens and user ids. As part of the login function, I generate a random number to be sent to the user as a cookie, then I hash that and use the c.Set functinon to set it (along with the user id) in go cache, and it adds over 900ms of latency to the page response. Most browsers expect a response within 500ms.

I don't know if this is expected (and if so if there are any ways to speed it up) or if I am doing something wrong, here is my code:

                // setup caching defaults
            sessionCache := cache.New(150*time.Hour, time.Hour)

                // generate cookietoken
        cookieToken := RandStringBytes(200)

        // create cookie with cookietoken
        ctx.SetCookie(
            "session",
            cookieToken,
            604800,
            "/",
            "",
            true,
            true,
        )

        // hash cookietoken for storage
        hashCookieToken, _ := HashPassword(cookieToken)

        // create session cache item with hashed cookietoken
        sessionCache.Set(hashCookieToken, user.ID, cache.DefaultExpiration)

Any help would be appreciated.

ghost commented 4 years ago

Decided to go with a different solution, this doesn't seem to really be designed for low-latency storage.