How do I use getSession() with md5?

Hey,
how can I cache into a User- Session (getSession()) like here:

$query->cache(function ($q) {
return ‘articles-’ . md5(serialize($q->clause(‘where’)));

?

If i do something like:
$ids = $query->toArray();
$this->request->getSession()->write( ‘my_array’, $ids );

it will not be possible to paginate the $ids…

Any Ideas?

I think what you’re trying to do is done nowadays with URL parameters instead of sessions. What would happen if someone opened two tabs to your site and needed to go through them independently? The session for one would overwrite the other and they’d be confused and annoyed. If details are in the URL instead, then the tabs operate fully independently, as they (almost always) should.

$query->cache(function ($q) {
return ‘articles-’ . md5(serialize($q->clause(‘where’)));

works fine but it will not be written into a private user session? or will it? i thought it was global if who ever will perform the search…

Sorry, you said you wanted to “cache into a user session”. Sessions are inherently private to the user. I’m probably not understanding what you’re trying to do.

if you do this:

$query->cache(function ($q) {
return ‘articles-’ . md5(serialize($q->clause(‘where’)));

it will be saved for everybody… means who ever will perform a search for a country e.g “china” will access the same cache.

if you do:

$this->request->getSession()->write( ‘my_array’, $ids );

it will be saved individually only for the user that is logged in. But beside your problem with the article id’s you will also not be able to paginate through them if you do some thing like:

$query = $this->Articles->find(‘all’, array(‘conditions’ => $conditions))->select([‘Articles.id’]);
$ids = $query->toArray();
$this->request->getSession()->write( ‘my_array’, $ids );

What I need is what this is doing:

$query->cache(function ($q) {
return ‘articles-’ . md5(serialize($q->clause(‘where’)));

But in a private Session… only for the logged in user.

Okay, I think I understand what you’re trying to accomplish. Am I right in assuming that the $conditions are in some way user-specific and that’s why you need different caching per user?

I haven’t this sort of query caching, I’ve used Cache::remember instead to save (usually highly processed) results under keys that I specify. Can you just add the user ID into your cache key, like return ‘articles-’ . $user_id . '-' . md5(...?

Or some other form of caching? Or save the IDs to the user session and build your own pagination that uses that array with array_slice?

All of this is assuming that you have actual performance problems that this caching is going to resolve for you. Too many people build something like this just because it’s something that they thought might be a good idea to cut down on database queries even though the site isn’t really all that busy and can easily handle the load.

you should do things right and you should expect to be successful! also it seems not to be more than two lines of code…

Can you just add the user ID into your cache key, like return ‘articles-’ . $user_id . '-' . md5(... ?

yes something like that is my question. I miss the ->write( ‘my_array’, $ids ) part where I can define it!

if you use:

$query->cache(function ($q) {return ‘articles-’ . md5(serialize($q->clause(‘where’)));

caching works fine and also pagination and thats only one line of code. It should be the same for individual users!

in my situation it should also work fine with global cache because no one else can perform the search again.

$query->cache(function ($q) {return ‘articles-’ . md5(serialize($q->clause(‘where’)));

saves the “where” clause (conditions). But you can’t win anything here, if you save large amounts of those clauses … so you need the individual session caching…

So, my proposal is something like

$query->cache(function ($q) use ($user) {
    return ‘articles-’ . $user->id . '-' . md5(serialize($q->clause(‘where’)));`
}

okay and the performance will be same like with:

$this->request->getSession()->write( ‘my_array’, $ids );

?

will it be saved in the cache like here: $this->request->getSession()->read( ‘my_array’) but the position/name ‘my_array’ will be “articles-’ . $user->id . ‘-’ . md5(serialize($q->clause(‘where’)”
or will every article be its own variable and not an array?