Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 03-05-2013, 04:16 AM
squidsk's Avatar
squidsk squidsk is offline
 
Join Date: Nov 2010
Posts: 969
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Cashing query results

I have a query that potentially gets called for each post, how do I cache the results so that the same query is not run multiple times for the same user? Within a fixed period of time anyways.
Reply With Quote
  #2  
Old 03-05-2013, 05:00 AM
nerbert nerbert is offline
 
Join Date: May 2008
Posts: 784
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Not that I'm any good at it but you could study up on $_SESSION

http://www.w3schools.com/php/php_sessions.asp

http://php.net/manual/en/reserved.variables.session.php
Reply With Quote
  #3  
Old 03-05-2013, 02:38 PM
squidsk's Avatar
squidsk squidsk is offline
 
Join Date: Nov 2010
Posts: 969
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Sessions are not for caching db results, and would slow down the site more than the db queries running themselves as every http header would have tonnes of session information.
Reply With Quote
  #4  
Old 03-05-2013, 02:42 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You could do something as simple as using a global variable array with the key being the userid and the value being whatever data you want to save (the query result or some other data you need from it). Then just check the array before you do a query, and if the data isn't there, do the query and save the results in the array.

ETA: ... but of cours ethat would just keep you from repeating the same query multiple times for one page request. Rereading what you asked, I guess you'd want something that would save the data for multiple page requests?
Reply With Quote
  #5  
Old 03-05-2013, 03:36 PM
squidsk's Avatar
squidsk squidsk is offline
 
Join Date: Nov 2010
Posts: 969
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by kh99 View Post
You could do something as simple as using a global variable array with the key being the userid and the value being whatever data you want to save (the query result or some other data you need from it). Then just check the array before you do a query, and if the data isn't there, do the query and save the results in the array.

ETA: ... but of cours ethat would just keep you from repeating the same query multiple times for one page request. Rereading what you asked, I guess you'd want something that would save the data for multiple page requests?
Yes, I'm not sure how to make use of the built in cache fucntions, similar to the forum cache, so that every query does not have to query the forum/forum permission tables.
Reply With Quote
  #6  
Old 03-05-2013, 06:58 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Well, the forum cache uses the datastore. You can use it by calling function build_datastore() like:
Code:
$mydata = array("some data", "more data");
build_datastore('mydata', serialize($mydata), 1); // '1' means automatically unserialize when reading

Then to load it, create a plugin using hook init_startup and code like:
Code:
if (THIS_SCRIPT = 'somepage')
{
   $new_datastore_fetch[] = 'mydata';
}
and your data will be in $vbulletin->mydata. Of course you can use more script names in the if, or leave it out completely, depending on what pages use your data.

Using the datastore that way will actually add one query to each page if no other products are using it on the same page. Also, obviously you can't do a query on the data. If you really have something you want to store that could be different for each user, then you probably don't want to use the datastore (unless you don't have many uses and aren't worried about it scaling).

There's another cache system used by the CMS, but I don't know the details of how you could use it. And I don't think it would eliminate extra queries either (well, at least it will require one to use it).

If your only goal is to eliminate any extra queries, you might be able to add your data to an exitsing table, or create a new table, then add it on to an existing query, depending on exactly what you're doing.
Reply With Quote
Благодарность от:
Lynne
  #7  
Old 03-05-2013, 09:15 PM
squidsk's Avatar
squidsk squidsk is offline
 
Join Date: Nov 2010
Posts: 969
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by kh99 View Post
If your only goal is to eliminate any extra queries, you might be able to add your data to an exitsing table, or create a new table, then add it on to an existing query, depending on exactly what you're doing.
That's exactly what I need to do. Now I just need to figure out how to change all the existing code to work through the showthread_query hook.

Edit: nvm, I'm not sure that will work because it will there is are potentially multiple pieces of information for each user, which likely means I can't use that query.
Reply With Quote
  #8  
Old 03-06-2013, 02:31 AM
nerbert nerbert is offline
 
Join Date: May 2008
Posts: 784
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by squidsk View Post
Sessions are not for caching db results, and would slow down the site more than the db queries running themselves as every http header would have tonnes of session information.
What do headers have to do with it? Session data is stored on the server and is available to your php scripting just like any other data.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:29 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.09404 seconds
  • Memory Usage 2,238KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_code
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (8)post_thanks_box
  • (1)post_thanks_box_bit
  • (8)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (8)post_thanks_postbit_info
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete