PHP Classes

File: SharedMemory.class.php

Recommend this page to a friend!
  Classes of beiliwenxiao   SharedMemory   SharedMemory.class.php   Download  
File: SharedMemory.class.php
Role: Class source
Content type: text/plain
Description: eAccelerator-based solutions for shared memory.
Class: SharedMemory
Cache content using eAccelerator extension
Author: By
Last change:
Date: 14 years ago
Size: 4,073 bytes
 

Contents

Class file image Download
<?php
/*
*Author: beiliwenxiao
*Name: eAccelerator-based solutions for shared memory.
*Version: 1.0
*Date: 2009-08-31
*Description:
*Can only be applied to a single primary key table.
*You must have eAccelerator.
*It can be reduce the database pressure and Ensure data accuracy and synchronization.
*You can be applied to High interactive webgame.
*/

/*
*Example:
*According to different sequential access
*
*test_get.php
*test_show.php
*test_del.php
*
*You can understand it how to work.
*And change the databese code with youself's,then you can use this at one page.
*/

class SharedMemory
{
   
//Your table primary key value
   
var $id;

   
//Your class name
   
var $classname;
   
   
//Your function name
   
var $funname;

   
//Your Data's id list
   
var $idlist;

   
//Need to remove a list of id
   
var $idlist_del;
       
   
//How many of each data processing
   
var $m = 1000;

   
//The number of seconds to suspend
   
var $n = 1;

    function
__construct($classname,$funname,$id='')
    {
       
$this->id=$id;
       
$this->classname=$classname;
       
$this->funname=$funname;
       
$this->idlist = eaccelerator_get($this->classname.'_'.$this->funname.'_idlist');
    }

    function
setData($data)
    {
        if (
$this->id<>0 and $this->id<>'')
        {
           
eaccelerator_lock($this->classname.'_'.$this->funname.'_'.$this->id);
           
eaccelerator_put($this->classname.'_'.$this->funname.'_'.$this->id,$data,'0');
           
eaccelerator_unlock($this->classname.'_'.$this->funname.'_'.$this->id);
           
            if (!
$this->idlist)
            {
               
$this->idlist[0]=$this->id;
               
               
eaccelerator_lock($this->classname.'_'.$this->funname.'_idlist');
               
eaccelerator_put($this->classname.'_'.$this->funname.'_idlist',$this->idlist,'0');
               
eaccelerator_unlock($this->classname.'_'.$this->funname.'_idlist');
            }
            else
            {
                if(!
in_array($this->id,$this->idlist))
                {
                   
array_push($this->idlist, $this->id);
                   
eaccelerator_lock($this->classname.'_'.$this->funname.'_idlist');
                   
eaccelerator_put($this->classname.'_'.$this->funname.'_idlist',$this->idlist,'0');
                   
eaccelerator_unlock($this->classname.'_'.$this->funname.'_idlist');
                }
            }

        }
    }

    function
getData($id='')
    {
        if (
$id<>'')
        {
           
$data = eaccelerator_get($this->classname.'_'.$this->funname.'_'.$id);
            if (!
$data)
            {
               
$data = call_user_func(array($this->classname, $this->funname),$id);
               
$this->setData($data);
            }
        }
        else
        {
           
$data = eaccelerator_get($this->classname.'_'.$this->funname.'_'.$this->id);
            if (!
$data)
            {
               
/*
                echo $this->classname;
                echo $this->funname;
                echo $this->id;
                exit();
                */
           
               
$data = call_user_func(array($this->classname, $this->funname),$this->id);
               
$this->setData($data);
            }
        }

        return
$data;

    }

    function
upData($data)
    {
       
eaccelerator_lock($this->classname.'_'.$this->funname.'_'.$this->id);
       
eaccelerator_put($this->classname.'_'.$this->funname.'_'.$this->id,$data,'0');
       
eaccelerator_unlock($this->classname.'_'.$this->funname.'_'.$this->id);
    }
   
    function
delData()
    {
       
eaccelerator_lock($this->classname.'_'.$this->funname.'_'.$this->id);
       
eaccelerator_rm($this->classname.'_'.$this->funname.'_'.$this->id);
       
eaccelerator_unlock($this->classname.'_'.$this->funname.'_'.$this->id);
       
call_user_func(array($this->classname, $this->funname.'_del'),$this->id);

    }

   
/*
    *
    *You need set regular implementation of the syncData();
    */
   
function syncData()
    {
        if (
$this->idlist)
        {
           
$c=count($this->idlist);
            for(
$i=0;$i<$c;$i++)
            {
               
$data=$this->getData($this->idlist[$i]);

               
call_user_func(array($this->classname, $this->funname.'_updata'),$this->idlist[$i],$data);

                if (
$i%$this->m == 0 )
                {
                   
sleep($this->n);
                }
               
            }

           
eaccelerator_lock($this->classname.'_'.$this->funname.'_idlist');
           
eaccelerator_rm($this->classname.'_'.$this->funname.'_idlist');
           
eaccelerator_unlock($this->classname.'_'.$this->funname.'_idlist');
        }

    }

}
?>