4

I am storing a lot of variables in session, which is creating a performance problem. So, I have been asked to store it somewhere else, I can store it in the database, but that would again be slow.

Is there any better alternative to store session variables? Global variable are per file/request. While cookies will open the variables to users and will not keep it server side.

Thanks in advance for your answers!

3 Answers 3

5

Consider memcached for semi-persistent data like this. Store the cache key in $_SESSION and then use it to grab your cached data.

Since memcached caches everything in memory (and is strictly a key-value store), it's faster than a database. It's somewhat ideal for things like sessions, because if you happen to lose the cached data, nothing serious is lost (the user just gets unexpectedly logged out).

In fact, the PHP Memcache implementation provides a session handler (see Example #2) which could transparently handle your sessions for you without you really needing to make any modifications to your code.

Sign up to request clarification or add additional context in comments.

4 Comments

can I use memcached to store custom key value variables? And is it maintained per user basis like session?
See what I edited in - PHP actually has a session handler that already implements that for you transparently, so you can keep using $_SESSION and simply get the performance benefit of memcached. But yes, you can also arbitrarily store key-value data in it, see the docs.
I am quit new to this, you mean to say the Example #2 in the link you mentioned will store my session variables using memcache? instead of normal session storage?
Correct, that is exactly what it does.
2

php sessions can be configured to work in many ways. you can run it from memcache if you have enough free memory on the server. that would be high performance

you can also use database to store sessions information but as you say this can be slow.

why is it currently creating performance problems? is there a large number of sessions being created or do you store large amounts of data in the session?

1 Comment

I store large amount of data in session
0

It depends on how many is "a lot" of variables? The default session storage for rails is using cookies, which is usually enough for me. The cookie is encrypted if you're worried about exposing the variables. Do you have to have it server side? With html5 you have localStorage options.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.