Re: Cache Elements for Different Users
The following component (+example controller using it) might help... I use it a lot. === http://bakery.cakephp.org/articles/view/ecache-easy-per-user-or-per-anything-cache-of-html-or-arrays Caching is basically required for any data-heavy or interaction-heavy site... but you need to be in control of who gets what cache; more specifically, you need to be sure people only get their own cached content. Cake 1.2 has caching... great caching if your contact is the same for everyone... but it's less than ideal for per-user content... in fact if you wanted to cache the public version of a view, and ignore cache userid; and even for one site an additional parameter: $this- repid. The cache file name defaults to starting with the current controller_action... but they can be arbitrarily set (which is how I use it most of the time). Feel free to add other parameters, if you need to split your caches by other things... for me, I just needed a unique cache per userid and per repid. Controller Class: ?php /*** * == Example of Usage == * The following is simply an example of how you might use the component. */ class MockUserController extends AppController { var $components = array('Ecache'); /** * This component doesn't handle authentication, nor does it automatically integrate with any other authenticaiton system. * You have to tell it what userid is logged in. * This shouldn't be too difficult, as most authenticaiton systems will have unique user ids. * If you don't have an authentication system, you probably don't need this component, as all you're cached data will look the same for all people. */ function beforeFilter() { // whatever your authentication sets $this-userid = $this-myAuthenticaitonSystem(); // populating value with a number, so we can discuss in this example. $this-userid = 42; // mockup value! // initialize Ecache $this-Ecache-startup($this); } /** * simple ecache example. just a regular database data result cached. * cache filename will be: ./app/tmp/cache/views/ users_list_0_42_0.php * if $this-userid was 89, the cache filename would be: ./app/tmp/ cache/views/users_list_0_89_0.php */ function example1() { // checking cache $d = $this-Ecache-ecache(null,'users','list'); // if missing cached content if (empty($d)) { // now get cached content $cond = array('User.parent_id'=$this-userid); $results = $this-User-findAll($cond); // writing cache $d = $this-Ecache-ecache($results,'users','list'); } $this-set('myusers',$d); } /** * there are 2 differnt cache calls in this example. * the first is like above, based on the $this-userid. * cache filename will be: ./app/tmp/cache/views/ users_pageelement_0_42_0.php * if $this-userid was 89, the cache filename would be: ./app/tmp/ cache/views/users_pageelement_0_89_0.php * the second will cache content globally (not on a per-user basis) * cache filename will be: ./app/tmp/cache/views/ users_pageelement_0_global_global.php * notice that the content in this example is generated from returned requestActions */ function example2() { // checking cache $d = $this-Ecache-ecache(null,'users','pageelement'); // if missing cached content if (empty($d)) { // now get cached content $results = $this-requestAction('/something/ action',array('return')); // writing cache $d = $this-Ecache- ecache($results,'users','pageelement'); } $this-set('user_specific_content',$d); // checking cache $d = $this-Ecache-ecache(null,'users','pageelement', 0,'global','global'); // if missing cached content if (empty($d)) { // now get cached content $results = $this-requestAction('/anything/ content',array('return')); // writing cache $d = $this-Ecache-ecache($results,'users','pageelement', 0,'global','global'); } $this-set('global_content',$d); } /** * This is simply a convenience wrapper for clearing the cache files. * clearing cache deletes: ./app/tmp/cache/view/* * ...often you may need to clear from a model, afterSave() */ function exampleClear() { $this-Ecache-clear(); } } ? Obviously, the above examples were simple. If you had a huge database query it might be worth it to cache, or if you used the same content a lot... most of the time though, caching is really useful for multiple database operations, heavily processed data, or returned requestAction data. Component Class: ?php /*** * PHP versions 4 and 5 * * ecache: extends and simplifies caching content based on per-user or per--anything parameters for the cakePHP framework. * Copyright (c)2007, Alan Blount * Licensed
Re: Cache Elements for Different Users
Ketan Patel, I am attempting to do the exact same thing and am running into trouble. How exactly are you setting the $userid variable? What I have done is in my view, set it up like this, $session-read('User.id), so the user id is set there, and then something like $this- element('tagCloud', array('cache'='+1 day', 'user_id'=$userid)... would that work for setting up a cached element for each user? And have you noticed if having possibly thousands of cached elements, one for each user, has caused a noticable slowdown? Please reply because I am contemplating on doing the same thing you have suggested. Thank you. On Jul 5, 8:45 am, Ketan Patel [EMAIL PROTECTED] wrote: Thanks Rajesh, to get me going in right direction.Ketan R. Rajesh Jeba Anbiah wrote: On Jul 5, 6:03 pm,KetanPatel[EMAIL PROTECTED] wrote: Currently I do ?php echo $this-element('tagCloud', array('cache'='+1 day')); ? Element 'tagCloud' : ?php $this-requestAction('controller'='tag', 'action'='generateTagCloud'); ? What I would want to do is to have a cached element individual to each user? So it could be like 'tagCloud_user1', 'tagCloud_user2',... so on. but I could n't find anything in the code that really does that? snip What about passing params? $this-element('tagCloud', array('cache'='+1 day', 'user_id'=10) -- ?php echo 'Just another PHP saint'; ? Email: rrjanbiah-at-Y!comBlog:http://rajeshanbiah.blogspot.com/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Cache Elements for Different Users
Well, technically when you cache something, you are creating a cache file which is a static html file. So if you have 1,000 users then it would create 1,000 separate cache files. This would not slow down if you have enough space on machine, as you would read in one file only per user. It is advisable to cache elements only if you are gaining significant savings in SQL queries and computational time. If you have single SQL query, then probably its not the best idea to cache element as it would not add much overhead. Its all about performance optimization. I have written a cake article for caching elements individually for each user but it is pending approval. The url is http://bakery.cakephp.org/articles/view/cache-elements-individually-for-each-user Hope I answered your questions, Ketan On Jul 13, 3:37 pm, inVINCable [EMAIL PROTECTED] wrote: Ketan Patel, I am attempting to do the exact same thing and am running into trouble. How exactly are you setting the $userid variable? What I have done is in my view, set it up like this, $session-read('User.id), so the user id is set there, and then something like $this-element('tagCloud', array('cache'='+1 day', 'user_id'=$userid)... would that work for setting up a cached element for each user? And have you noticed if having possibly thousands of cached elements, one for each user, has caused a noticable slowdown? Please reply because I am contemplating on doing the same thing you have suggested. Thank you. On Jul 5, 8:45 am, Ketan Patel [EMAIL PROTECTED] wrote: Thanks Rajesh, to get me going in right direction.Ketan R. Rajesh Jeba Anbiah wrote: On Jul 5, 6:03 pm,KetanPatel[EMAIL PROTECTED] wrote: Currently I do ?php echo $this-element('tagCloud', array('cache'='+1 day')); ? Element 'tagCloud' : ?php $this-requestAction('controller'='tag', 'action'='generateTagCloud'); ? What I would want to do is to have a cached element individual to each user? So it could be like 'tagCloud_user1', 'tagCloud_user2',... so on. but I could n't find anything in the code that really does that? snip What about passing params? $this-element('tagCloud', array('cache'='+1 day', 'user_id'=10) -- ?php echo 'Just another PHP saint'; ? Email: rrjanbiah-at-Y!comBlog:http://rajeshanbiah.blogspot.com/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Cache Elements for Different Users
Awesome. I was looking for a way to do that. Thanks for the input. On 7/5/07, Ketan Patel [EMAIL PROTECTED] wrote: Found a way to do it using cake without modifying the core libs. This is a workaround, but does the job. $this-element('tagCloud', array('cache'='+1 day', 'plugin'= $userId)); so this would create the file 'element_$userId_tagCloud' in the cache folder for views. Now to delete it, $this-clearCache('element_$userId_tagCloud', 'views', ''); I am faking plugin to be the userid, but this works and does my job. Hopefully, this might help someone else. Cheers, Ketan ;-) Chris Hartjes wrote: On 7/5/07, Ketan Patel [EMAIL PROTECTED] wrote: Can some one pls shed some light here. I don't think Cake can help you out there. What you might have to do is code your element in such a way that it does the caching itself based on a unique ID. You might consider a small file-based caching system. Not easy, but what you're asking for isn't easy. -- Chris Hartjes Senior Developer Cake Development Corporation My motto for 2007: Just build it, damnit! @TheBallpark - http://www.littlehart.net/attheballpark @TheKeyboard - http://www.littlehart.net/atthekeyboard --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Cache Elements for Different Users
On 7/5/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Is there any scope in an enhancement to the Cake cache, that allowed you to bind certain session variables into the cache hash. Eg. you could setup the cache for certain views to be cached with an md5 of specified session variables. I suggest you go to trac.cakephp.org and submit the enhancement request. No guarantees it will be accepted, but you never know. -- Chris Hartjes Senior Developer Cake Development Corporation My motto for 2007: Just build it, damnit! @TheBallpark - http://www.littlehart.net/attheballpark @TheKeyboard - http://www.littlehart.net/atthekeyboard --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Cache Elements for Different Users
Thought it could do with some discussion before going onto trac, so we could clean up any debate over it, but yeah, will get round to that soon. simon On Jul 5, 2:15 pm, Chris Hartjes [EMAIL PROTECTED] wrote: On 7/5/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Is there any scope in an enhancement to the Cake cache, that allowed you to bind certain session variables into the cache hash. Eg. you could setup the cache for certain views to be cached with an md5 of specified session variables. I suggest you go to trac.cakephp.org and submit the enhancement request. No guarantees it will be accepted, but you never know. -- Chris Hartjes Senior Developer Cake Development Corporation My motto for 2007: Just build it, damnit! @TheBallpark -http://www.littlehart.net/attheballpark @TheKeyboard -http://www.littlehart.net/atthekeyboard --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Cache Elements for Different Users
On 7/5/07, Ketan Patel [EMAIL PROTECTED] wrote: Can some one pls shed some light here. I don't think Cake can help you out there. What you might have to do is code your element in such a way that it does the caching itself based on a unique ID. You might consider a small file-based caching system. Not easy, but what you're asking for isn't easy. -- Chris Hartjes Senior Developer Cake Development Corporation My motto for 2007: Just build it, damnit! @TheBallpark - http://www.littlehart.net/attheballpark @TheKeyboard - http://www.littlehart.net/atthekeyboard --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Cache Elements for Different Users
Currently I do ?php echo $this-element('tagCloud', array('cache'='+1 day')); ? Element 'tagCloud' : ?php $this-requestAction('controller'='tag', 'action'='generateTagCloud'); ? What I would want to do is to have a cached element individual to each user? So it could be like 'tagCloud_user1', 'tagCloud_user2',... so on. but I could n't find anything in the code that really does that? Can some one pls shed some light here. Thanks, Ketan Patel wrote: Is there a way to cache elements separately for each user? Currently, since the element name is same, the cache is basically overwritten, I would prefer to cache the element individually. So that later on, I can delete the cache when an update is performed for that user, otherwise it is uses the cached view. Thanks --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Cache Elements for Different Users
Is there any scope in an enhancement to the Cake cache, that allowed you to bind certain session variables into the cache hash. Eg. you could setup the cache for certain views to be cached with an md5 of specified session variables. It would solve this problem, and probably make quite a nice feature for other sites and applications as well. Just an idea. Simon On Jul 5, 2:08 pm, Chris Hartjes [EMAIL PROTECTED] wrote: On 7/5/07, Ketan Patel [EMAIL PROTECTED] wrote: Can some one pls shed some light here. I don't think Cake can help you out there. What you might have to do is code your element in such a way that it does the caching itself based on a unique ID. You might consider a small file-based caching system. Not easy, but what you're asking for isn't easy. -- Chris Hartjes Senior Developer Cake Development Corporation My motto for 2007: Just build it, damnit! @TheBallpark -http://www.littlehart.net/attheballpark @TheKeyboard -http://www.littlehart.net/atthekeyboard --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Cache Elements for Different Users
On Jul 5, 6:03 pm, Ketan Patel [EMAIL PROTECTED] wrote: Currently I do ?php echo $this-element('tagCloud', array('cache'='+1 day')); ? Element 'tagCloud' : ?php $this-requestAction('controller'='tag', 'action'='generateTagCloud'); ? What I would want to do is to have a cached element individual to each user? So it could be like 'tagCloud_user1', 'tagCloud_user2',... so on. but I could n't find anything in the code that really does that? snip What about passing params? $this-element('tagCloud', array('cache'='+1 day', 'user_id'=10) -- ?php echo 'Just another PHP saint'; ? Email: rrjanbiah-at-Y!comBlog: http://rajeshanbiah.blogspot.com/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Cache Elements for Different Users
Found a way to do it using cake without modifying the core libs. This is a workaround, but does the job. $this-element('tagCloud', array('cache'='+1 day', 'plugin'= $userId)); so this would create the file 'element_$userId_tagCloud' in the cache folder for views. Now to delete it, $this-clearCache('element_$userId_tagCloud', 'views', ''); I am faking plugin to be the userid, but this works and does my job. Hopefully, this might help someone else. Cheers, Ketan ;-) Chris Hartjes wrote: On 7/5/07, Ketan Patel [EMAIL PROTECTED] wrote: Can some one pls shed some light here. I don't think Cake can help you out there. What you might have to do is code your element in such a way that it does the caching itself based on a unique ID. You might consider a small file-based caching system. Not easy, but what you're asking for isn't easy. -- Chris Hartjes Senior Developer Cake Development Corporation My motto for 2007: Just build it, damnit! @TheBallpark - http://www.littlehart.net/attheballpark @TheKeyboard - http://www.littlehart.net/atthekeyboard --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Cache Elements for Different Users
Thanks Rajesh, to get me going in right direction. Ketan R. Rajesh Jeba Anbiah wrote: On Jul 5, 6:03 pm, Ketan Patel [EMAIL PROTECTED] wrote: Currently I do ?php echo $this-element('tagCloud', array('cache'='+1 day')); ? Element 'tagCloud' : ?php $this-requestAction('controller'='tag', 'action'='generateTagCloud'); ? What I would want to do is to have a cached element individual to each user? So it could be like 'tagCloud_user1', 'tagCloud_user2',... so on. but I could n't find anything in the code that really does that? snip What about passing params? $this-element('tagCloud', array('cache'='+1 day', 'user_id'=10) -- ?php echo 'Just another PHP saint'; ? Email: rrjanbiah-at-Y!comBlog: http://rajeshanbiah.blogspot.com/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---