Jimbos Posted November 14, 2023 Share Posted November 14, 2023 Hello, I'm trying to cache some queries. Everything works correctly, but I encounter issues when I try to insert one or more external variables into the function: $name = $cache->get('cachedname', "+2 minutes", function($pages) { return $pages->find("template=names, parent=1195, id=$nameid, status=published, limit=3"); }); Can you help me? Link to comment Share on other sites More sharing options...
poljpocket Posted November 14, 2023 Share Posted November 14, 2023 (edited) I would assume the $nameid variable is not available in your closure unless you add use ($nameid) to include it. $pages is available only because WireCache supports optionally passing API variables as arguments. This should work: $name = $cache->get('cachedname', "+2 minutes", function($pages) use($nameid) { return $pages->find("template=names, parent=1195, id=$nameid, status=published, limit=3"); }); Here is some documentation about closures: PHP: Anonymous functions - Manual Edited November 14, 2023 by poljpocket add docs link 1 Link to comment Share on other sites More sharing options...
poljpocket Posted November 14, 2023 Share Posted November 14, 2023 Also, your cache function only works as expected if the $nameid variable is the same every time you use a cached version of the query. To get around this, you should make the cache key depend on it, e.g.: $name = $cache->get("cachedname-$nameid", "+2 minutes", function($pages) use($nameid) { return $pages->find("template=names, parent=1195, id=$nameid, status=published, limit=3"); }); 2 Link to comment Share on other sites More sharing options...
Jimbos Posted November 14, 2023 Author Share Posted November 14, 2023 Thank you polj!!! It works now. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now