Jump to content

Very slow $cache


SqANT
 Share

Recommended Posts

Hey! I need a json file with members and it is quite slow

$members = $cache->get("members", "+10 minutes", function($users) {
	return $users->find('roles=member, sort=lastname, limit=3000');
});

This one takes about 8 seconds to load, regardless if it is cached! I dont know where the cache file is, but I am sure it is loading from the cache, because changing limit=30 won't affect the output until I delete the cache or wait 10 minutes.

Where is the mistake?

 

Link to comment
Share on other sites

I did a quick test here. I created the cache and a loop to show all 3k users emails on a local installation with 7k+ users and it took around 3.5s (measured with TracyDebugger) and half of that after the cache was created. You can use the module ClearCacheAdmin to see all the caches created by PW on a nice list on admin.

Actually, it seems findMany is not implemented on $users. Does anybody confirm that?

Link to comment
Share on other sites

On 1/27/2019 at 6:36 AM, SqANT said:

Hey! I need a json file with members and it is quite slow


$members = $cache->get("members", "+10 minutes", function($users) {
	return $users->find('roles=member, sort=lastname, limit=3000');
});

 

Your cache function doesn't return JSON though, but rather a PageArray with the full user objects. I suggest only returning the JSON data you need to see if that makes a difference.

2 hours ago, Sergio said:

Actually, it seems findMany is not implemented on $users. Does anybody confirm that?

That's correct. A solution is to use $pages->findMany() with the user template.

$members = $cache->get('members', '+10 minutes', function($pages) {
	$member_pages = $pages->findMany('template=user, roles=member, sort=lastname, limit=3000, check_access=0');
	return json_encode($member_pages->explode(['name', 'email'])); // Whatever fields/properties you need from the member pages
});

 

  • Like 4
Link to comment
Share on other sites

On 1/27/2019 at 6:39 PM, Sergio said:

I did a quick test here. I created the cache and a loop to show all 3k users emails on a local installation with 7k+ users and it took around 3.5s (measured with TracyDebugger) and half of that after the cache was created. You can use the module ClearCacheAdmin to see all the caches created by PW on a nice list on admin.

Actually, it seems findMany is not implemented on $users. Does anybody confirm that?

I had similar results with just one field. I had just a quick look at it, but it seems findMany isn't much beneficial here, because I access all data anyway.

On 1/27/2019 at 9:38 PM, Robin S said:

Your cache function doesn't return JSON though, but rather a PageArray with the full user objects. I suggest only returning the JSON data you need to see if that makes a difference.

That's correct. A solution is to use $pages->findMany() with the user template.


$members = $cache->get('members', '+10 minutes', function($pages) {
	$member_pages = $pages->findMany('template=user, roles=member, sort=lastname, limit=3000, check_access=0');
	return json_encode($member_pages->explode(['name', 'email'])); // Whatever fields/properties you need from the member pages
});

 

I had cached the JSON object at first, this was just the last code I used. I read in a blog post you can cache an object as well. I now cached just the array and convert it to json afterwards. This has the best results (ttfb ~1500ms).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...