View previous topic :: View next topic |
Author |
Message |
dmDan Smarty n00b
Joined: 21 Mar 2012 Posts: 4
|
Posted: Wed Mar 21, 2012 12:04 pm Post subject: Cache and isCached |
|
|
Hello, i've used smarty for a long time now and it's great!
However I now want to make more use of the caching features and have hit a bit of a stumbling block with the way you do it.
I have a function that processes incoming web requests and I want to check the cache at this level (i.e. the very top) before I run any other code.
The issue I have is the isCached function requires a template, as the system is dumb to a point it will not know what template is used when it calls to see if it item is cached, I only have an id generated from the requested url.
Has anyone else made the cache work this way off of just unique cache id's?
Personally I think this makes more sense to have a unique id per cache and not rely on knowing the template (that should be an optional param) - especially if you use a hash to make the unique id chances of using the the same id are minimal. Also means that I need not run any more logic/code to display the request resulting in a faster response for the user.
If you can do this already then can someone give me a hint otherwise what are your thoughts on this?
Thanks,
Dan |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Mar 21, 2012 4:26 pm Post subject: |
|
|
the cache always uses the path to the template as the default id, and the cache_id is additive to this. If the template path is not going to be changing between similar requests, then using a cache_id for each request should work fine. |
|
Back to top |
|
dmDan Smarty n00b
Joined: 21 Mar 2012 Posts: 4
|
Posted: Wed Mar 21, 2012 5:22 pm Post subject: |
|
|
As I said I don't know what the template is at this stage so it will trigger this error:
Code: |
Unable to parse resource name ""
Trace is:
smarty3.1/libs/sysplugins/smarty_internal_template.php [ ]
smarty3.1/libs/sysplugins/smarty_cacheresource.php [ 289 ] » __get()
__get(
source)
smarty3.1/libs/sysplugins/smarty_internal_template.php [ 636 ] » __construct()
__construct()
smarty3.1/libs/sysplugins/smarty_internal_templatebase.php [ 386 ] » __get()
__get(cached)
lib/controller/webpage.php [ 388 ] » isCached()
isCached(null, f2973ce6becf3c2331f3adb6dc76432144832e4a);
|
..this is my code form this step which is irreverent.
Which is obvious cause I do the check like this:
Code: |
$template = null;
$id = sha1({*MY REF*});
$this->isCached($template,$id);
|
I guess the best bet is to make a custom cache controller?
I see that it uses Smarty_Internal_CacheResource_File by default(?) but obviously this is not going to work how I want it as it still requires the template.
Do I need to customise any of the other functions apart from the fetch, save and delete? I take it that smarty will still work the same way and process the cache the same way and add any includes etc? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Mar 21, 2012 5:28 pm Post subject: |
|
|
A cache file is/must always be related to a template.
It can't work without..... |
|
Back to top |
|
dmDan Smarty n00b
Joined: 21 Mar 2012 Posts: 4
|
Posted: Wed Mar 21, 2012 5:48 pm Post subject: |
|
|
Why should I need to know the template?
As all I want to do is:
1. Check a directory to see if a filename matching the cache_id exists (all unique sha1 hashes).
2. Check that file is in the stated cache period.
3. Load it, or remake after the rest code has been processed.
Pretty simple really, no need to go looking up what template that file was created from at the beginning (we set the template at the end if the cache was not found).
After all the idea of a cache is to speed up the site and reduce the resource used and looking up the template used is a waste of time. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Mar 21, 2012 6:13 pm Post subject: |
|
|
This would brake a lot of other features like compile checking. A cache file is not only invalid if it expires but also if the template sour was modified.
The cache_id is in addition to the template hash and needed when same template is cached with different content for example with product pages.
Changing that logic will screwup the Smarty internals.
If you want something simpler use fetch() to render content and write it to your own files. |
|
Back to top |
|
dmDan Smarty n00b
Joined: 21 Mar 2012 Posts: 4
|
Posted: Tue Mar 27, 2012 3:27 pm Post subject: |
|
|
Found a way round it using a custom Smarty_CacheResource_Custom class.
It seems to work fine, I see what you all meant by needing to know the the template, well I found a way round that.
A suggestion maybe that the isCached function calls a isCached inside this class, to make the lookup a bit quicker as it ends up calling the fetch in the examples from the process. |
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Tue Mar 27, 2012 3:39 pm Post subject: |
|
|
dmDan wrote: | A suggestion maybe that the isCached function calls a isCached inside this class, to make the lookup a bit quicker as it ends up calling the fetch in the examples from the process. |
That is not possible, as your suggestion would open the hole for a race condition. _________________ Twitter |
|
Back to top |
|
|