Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Cache and isCached

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
dmDan
Smarty n00b


Joined: 21 Mar 2012
Posts: 4

PostPosted: Wed Mar 21, 2012 12:04 pm    Post subject: Cache and isCached Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7366
Location: Lincoln Nebraska, USA

PostPosted: Wed Mar 21, 2012 4:26 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
dmDan
Smarty n00b


Joined: 21 Mar 2012
Posts: 4

PostPosted: Wed Mar 21, 2012 5:22 pm    Post subject: Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Wed Mar 21, 2012 5:28 pm    Post subject: Reply with quote

A cache file is/must always be related to a template.
It can't work without.....
Back to top
View user's profile Send private message
dmDan
Smarty n00b


Joined: 21 Mar 2012
Posts: 4

PostPosted: Wed Mar 21, 2012 5:48 pm    Post subject: Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Wed Mar 21, 2012 6:13 pm    Post subject: Reply with quote

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
View user's profile Send private message
dmDan
Smarty n00b


Joined: 21 Mar 2012
Posts: 4

PostPosted: Tue Mar 27, 2012 3:27 pm    Post subject: Reply with quote

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
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Tue Mar 27, 2012 3:39 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3 All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP