SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Hold PHP variables RAM or making variables available to the application as a whole?

    Hi Sitepointer,

    I have written a website template system for multilingual websites. It's fairly simple.

    There are 3 database tables, placeholder, placeholder_language and language (see structure below).

    In my template I replace a word in squared brackets [example] with what ever that word is in the users selected language.

    using str_replace I replace the bracketed word [example] for 'example' if the user is viewing the site in English and 'ejemplo' if the user is viewing the site in Spanish.

    The placeholders are parsed is done on every page load. My worry is that this will cause excessive load with multiple users.

    Since these variables don't change is there a way of holding them in RAM or making them available as variables to the application as a whole?

    Many thanks. Muchas Gracias.

    Tables follow.

    CREATE TABLE IF NOT EXISTS `placeholder` (
    `placeholder_id` int(11) NOT NULL AUTO_INCREMENT,
    `placeholder_name` varchar(255) NOT NULL,
    PRIMARY KEY (`placeholder_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0;


    CREATE TABLE IF NOT EXISTS `placeholder_language` (
    `placeholder_language_id` int(11) NOT NULL AUTO_INCREMENT,
    `placeholder_id` int(11) NOT NULL,
    `language_id` int(11) NOT NULL,
    `placeholder_language_name` varchar(255) CHARACTER SET utf8 NOT NULL,
    PRIMARY KEY (`placeholder_language_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;

    CREATE TABLE IF NOT EXISTS `language` (
    `language_id` int(11) NOT NULL AUTO_INCREMENT,
    `language_name` varchar(255) NOT NULL
    PRIMARY KEY (`language_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;

  2. #2
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have you thought about storing them in $_SESSION ?

  3. #3
    Team SitePoint santouras's Avatar
    Join Date
    Jul 2006
    Location
    planet earth
    Posts
    276
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Storing them as _SESSION variables will affect each user between page loads, not across multiple users accessing the system at the same time.

    As long as your tables are properly indexed and you're not hitting the DB for each individual replacement term, you are not going to incur massive overhead by going to the DB multiple times. If you are concerned, you might look into using APC for caching, or even using a translation file for storing each language instead of a database.
    my utility belt tells me its to the bar batman

    read the manual then google it then do a search THEN post....

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by santouras View Post
    Storing them as _SESSION variables will affect each user between page loads, not across multiple users accessing the system at the same time.

    As long as your tables are properly indexed and you're not hitting the DB for each individual replacement term, you are not going to incur massive overhead by going to the DB multiple times. If you are concerned, you might look into using APC for caching, or even using a translation file for storing each language instead of a database.
    Yup, +1. If you want them in memory, a properly indexed table / query is really the only way to go.

  5. #5
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,036
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    MySQL and most other DB engines have memcaches for the results of the most frequently ran queries, so without even touching the PHP code you can increase caching of the results by increasing the db engines' cache size. Within PHP itself there is APC and memcache. A final trick is to mount a RAM disk on your server and have PHP write to a file on that ram disk.

  6. #6
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the function I use to replace the placeholders.

    PHP Code:
    function  replace_placeholders($output){
        
    //replace placeholders
        
    if (!empty($_GET['language_id'])) {
        
    $_SESSION['language_id'] = $_GET['language_id'];
        }
        if (isset(
    $_SESSION['language_id'])) {
        
    $language_id $_SESSION['language_id'];
        }
        else {
        
    $language_id '1';
        }
        
    $query "SELECT placeholder_name, placeholder_language_name FROM placeholder, placeholder_language 
        WHERE placeholder.placeholder_id = placeholder_language.placeholder_id 
        AND language_id = '
    $language_id'";
        
        
    $result mysql_query($query);
        
        
    $pln_array = array();
        
    $pn_array = array();
        
        while (
    $array mysql_fetch_assoc($result)) {
            
    $placeholder_name $array['placeholder_name'];
            
    $placeholder_language_name ucfirst($array['placeholder_language_name']);
            
            
    array_push($pln_array,$placeholder_language_name);
            
    array_push($pn_array,$placeholder_name);
            }
            
    $output str_replace($pn_array,$pln_array,$output);
        return 
    $output;


  7. #7
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Austin
    Posts
    401
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    MySQL and most other DB engines have memcaches for the results of the most frequently ran queries, so without even touching the PHP code you can increase caching of the results by increasing the db engines' cache size. Within PHP itself there is APC and memcache. A final trick is to mount a RAM disk on your server and have PHP write to a file on that ram disk.
    I would go this route as well. You can also manually store via APC and memcached as well. APC does a pretty good job of automatic caching with minimal configuration.
    Merchant Equipment Store - Merchant Services, POS, Equipment, and supplies.
    Merchant Account Blog | Ecommerce Blog

  8. #8
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    The problem with application scoping and PHP is that one cannot tell is a given session is the first or last, in order to facilitate proper cleanup. While you can write a counter to file, you probably shouldn't. Also, keep in mind the following things:

    1) Memory is not share between machines, therefore, a later move to a cluster could potentially cause problems.
    2) Users of different sessions may have selected different languages/locales, and sharing this across all sessions would pose problems.

    I post this information as further support for the db solution.

    What I would do, however, is group certain phrases into phrasegroups in order to facilitate seletion of only the conversions you'll need. E.G. put all common phrases in a group, all phrases used on the login screen in a group.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •