note 61692 modified in ref.shmop by danbrown
| From: | danbrown@php.net | Date: | Thu, 21 May 2009 15:05:43 +0000 |
| Subject: | note 61692 modified in ref.shmop by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155845@lists.php.net to get a copy of this message | ||
I wrote a php memcache back in 2003 as a sort of proof of concept
it is use on a few machines for doing heavy page load caching...
it works very well.
Following are some of the core functions I made
<?php
###############################################
#### shared mem functions
/*
for debugging these
use
ipcs to view current memory
use ipcrm -m {shmid} to remove
on some systems use ipcclean to clean up unused memory if you
don't want to do it by hand
*/
###############################################
function get_key($fsize, $file){
if(!file_exists(TMPDIR.TMPPRE.$file)){
touch(TMPDIR.TMPPRE.$file);
}
$shmkey = @shmop_open(ftok(TMPDIR.TMPPRE.$file, 'R'), "c", 0644,
$fsize);
if(!$shmkey) {
return false;
}else{
return $shmkey;
}//fi
}
function writemem($fdata, $shmkey){
if(MEMCOMPRESS && function_exists('gzcompress')){
$fdata = @gzcompress($fdata, MEMCOMPRESSLVL);
}
$fsize = strlen($fdata);
$shm_bytes_written = shmop_write($shmkey, $fdata, 0);
updatestats($shm_bytes_written, "add");
if($shm_bytes_written != $fsize) {
return false;
}else{
return $shm_bytes_written;
}//fi
}
function readmem($shmkey, $shm_size){
$my_string = @shmop_read($shmkey, 0, $shm_size);
if(MEMCOMPRESS && function_exists('gzuncompress')){
$my_string = @gzuncompress($my_string);
}
if(!$my_string) {
return false;
}else{
return $my_string;
}//fi
}
function deletemem($shmkey){
$size = @shmop_size($shmkey);
if($size > 0){ updatestats($size, "del"); }
if(!@shmop_delete($shmkey)) {
@shmop_close($shmkey);
return false;
}else{
@shmop_close($shmkey);
return true;
}
}
function closemem($shmkey){
if(!shmop_close($shmkey)) {
return false;
}else{
return true;
}
}
function iskey($size, $key){
if($ret = get_key($size, $key)){
return $ret;
}else{
return false;
}
}
################################################
?>
--was--
I wrote a php memcache back in 2003 as a sort of proof of concept
it is use on a few machines for doing heavy page load caching...
it works very well.
Following are some of the core functions I made
<?
###############################################
#### shared mem functions
/*
for debugging these
use ipcs to view current memory
use ipcrm -m {shmid} to remove
on some systems use ipcclean to clean up unused memory if you
don't want to do it by hand
*/
###############################################
function get_key($fsize, $file){
if(!file_exists(TMPDIR.TMPPRE.$file)){
touch(TMPDIR.TMPPRE.$file);
}
$shmkey = @shmop_open(ftok(TMPDIR.TMPPRE.$file, 'R'), "c", 0644,
$fsize);
if(!$shmkey) {
return false;
}else{
return $shmkey;
}//fi
}
function writemem($fdata, $shmkey){
if(MEMCOMPRESS && function_exists('gzcompress')){
$fdata = @gzcompress($fdata, MEMCOMPRESSLVL);
}
$fsize = strlen($fdata);
$shm_bytes_written = shmop_write($shmkey, $fdata, 0);
updatestats($shm_bytes_written, "add");
if($shm_bytes_written != $fsize) {
return false;
}else{
return $shm_bytes_written;
}//fi
}
function readmem($shmkey, $shm_size){
$my_string = @shmop_read($shmkey, 0, $shm_size);
if(MEMCOMPRESS && function_exists('gzuncompress')){
$my_string = @gzuncompress($my_string);
}
if(!$my_string) {
return false;
}else{
return $my_string;
}//fi
}
function deletemem($shmkey){
$size = @shmop_size($shmkey);
if($size > 0){ updatestats($size, "del"); }
if(!@shmop_delete($shmkey)) {
@shmop_close($shmkey);
return false;
}else{
@shmop_close($shmkey);
return true;
}
}
function closemem($shmkey){
if(!shmop_close($shmkey)) {
return false;
}else{
return true;
}
}
function iskey($size, $key){
if($ret = get_key($size, $key)){
return $ret;
}else{
return false;
}
}
################################################
?>
http://php.net/manual/en/ref.shmop.php