Jump to content

Caching queries


Recommended Posts

Hi there,

As I'm starting to work across pages, I'm seeing some cases where I might like to re-use queries or HTML fragments that in my original framework, I may have cached in properties of more globally accessible object references.

In templates, you're in a page-centric scenario and I wondered whether there is anything in the framework for stashing values for use across template processing?


Link to comment
Share on other sites

Hi Pete,

Many thanks for responding! :)

I don't think I made myself clear. What I was referring to was caching the runtime results of queries, not their definitions, and possibly HTML fragments (generated on the fly) that might be used more than once in the same server-side hit.

Know what I mean?



Link to comment
Share on other sites

See the $session variable. You can store objects, but always have to be careful, like with objects that may reference others and ensuring those object's class definitions are present before they are instantiated in the session. ProcessWire's $session isn't much more than a wrapper on top of PHP's $_SESSION, and you can still use PHP's $_SESSION if you want to. More about objects in sessions here. Personally, I prefer to recreate objects myself on each request. It should be possible to do what you are asking to do but the details of it will be PHP-specific rather than ProcessWire-specific.

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By Mirza
      I have built a system in processwire, which has more than 600K pages.
      A team of 40 people is using the system, DB is from AWS with 16GB Ram.
      But still, select queries are getting locked.
      It would be great if someone suggests how to solve this problem.
      Also note: We have around 48 fields in one template.
      Thanks in advance.

    • By FireWire
      I'm building a site that renders certain features using reusable classes. Unfortunately FileCompiler caches the files and my changes don't show up unless I clear the FileCompiler cache.
      On top of that, when I try to use the wire() function inside a class it throws a "Fatal error: Uncaught Error: Call to undefined function wire()". I've tried playing with ProcessWire namespacing, but have come up short.
      My files are structured as such:
      app.php - Is included in my templates and loads classes as they're called with spl_autoload_register.
      - classes/ExampleClass.class.php - Loaded by app.php
    • By Brian Scramlin
      I am truly stuck. I have a website I built for a client that has become quite successful: http://drydenwire.com
      It is an independent news agency. The problem is, I built everything from scratch in Processwire and things were purring along until the traffic went up. Now, I keep getting these huge spikes which last about 15 seconds and cause all users to receive a 503 Error. I have created a public repo for review: https://github.com/scramlo/DrydenWire.com/tree/master and can answer any questions that may lead to an answer. I am willing to hire a more experienced developer if the community is unable to help me mine out this issue.
      I have gone through the usage logs and know that all traffic is legitimate, I am not getting bombarded by a spam IP address or anything like that.
      Thank you for any and all assistance as this client is getting frustrated and I would rather not lose them!!!

    • By Doc
      I'm building my homepage, it contains ~60 custom fields (lines or words I define & translate in the admin in different languages).
      The homepage is dynamic and my users are logged.
      I'm trying to avoid many MySQL queries to retrieve the fields I've setup in the admin, I think I can do better than 60 different queries to display that content on the home.
      Regarding performance, will markup cache a good approach here ?
      I understand it makes sense when you have a scrolldown menu which is built within a loop for example but is it still good if I cache 60x a word or a group of words in my code ?
      Another idea I had was to dynamically determine which language is used by the user and load a language file which contains the translated text (uk.inc.php) and so on... (less elegant I think, but I'll use that anyway for dynamic expressions such as "you have %nb items in your cart").
    • By mr-fan
      Just a small question on selector performance and room for improvments...
      i use a little function that renders me all archiv links like /2015/   /2014/ and so on...all years that are _used_ in the article system...
      This cause a selector without limit...and there will be many posts/articles in future now there are about 40 - it will scale up to 100 fast and more will follow...so here is the question about caching the find query or using other code...?
      Basic information i work with a URL segment approach that show me /year/ and /category/ overview pages.
      (Category or not the problem while here limit works and pagination is used)
      Code from the year archive:
      function renderArchive() { //check for url segments $seg1 = wire('sanitizer')->pageName(wire('input')->urlSegment1); //get all posts to check for years $posts = wire('pages')->find("template=artikel"); //get homepage url for the links $homepageurl = wire('pages')->get('/')->httpUrl; //get article root page for links $artikel_root = wire('pages')->get(1068); //get year list $out = '<h4 class="subtitle">Archiv</h4>'; $out .= '<div class="listbox1"><ul>'; //Setup for the year sidemenu $years = array(); // find the array of years for all events foreach ($posts as $y) { $dateY = $y->getUnformatted("publish_from"); $years[]= date('Y',(int)$dateY); } $years = array_unique($years); arsort($years); $year = ""; foreach($years as $key => $year) { // Output the year $class = $year === $seg1 ? " active" : ''; $out .= '<li class="'.$class.'"><a href="'.$homepageurl.$artikel_root->name.'/'.$year.'/">'.$year.'</a></li>'; } //close list $out .= '</ul></div>'; return $out; } Could this done better - or cached somehow (I'm not really experienced with caching such things)
      regards mr-fan
  • Create New...