Memcache 中实现消息队列

 更新时间:2016年11月25日 16:30  点击:1940


class Memcache_Queue
{
private $memcache;
private $name;
private $prefix;
function __construct($maxSize, $name, $memcache, $prefix = "__memcache_queue__")
{
if ($memcache == null) {
throw new Exception("memcache object is null, new the object first.");
}
$this->memcache = $memcache;
$this->name = $name;
$this->prefix = $prefix;
$this->maxSize = $maxSize;
$this->front = 0;
$this->real = 0;
$this->size = 0;
}
function __get($name)
{
return $this->get($name);
}
function __set($name, $value)
{
$this->add($name, $value);
return $this;
}
function isEmpty()
{
return $this->size == 0;
}
function isFull()
{
return $this->size == $this->maxSize;
}
function enQueue($data)
{
if ($this->isFull()) {
throw new Exception("Queue is Full");
}
$this->increment("size");
$this->set($this->real, $data);
$this->set("real", ($this->real + 1) % $this->maxSize);
return $this;
}
function deQueue()
{
if ($this->isEmpty()) {
throw new Exception("Queue is Empty");
}
$this->decrement("size");
$this->delete($this->front);
$this->set("front", ($this->front + 1) % $this->maxSize);
return $this;
}
function getTop()
{
return $this->get($this->front);
}
function getAll()
{
return $this->getPage();
}
function getPage($offset = 0, $limit = 0)
{
if ($this->isEmpty() || $this->size < $offset) {
return null;
}
$keys[] = $this->getKeyByPos(($this->front + $offset) % $this->maxSize);
$num = 1;
for ($pos = ($this->front + $offset + 1) % $this->maxSize; $pos != $this->real; $pos = ($pos + 1) % $this->maxSize)
{
$keys[] = $this->getKeyByPos($pos);
$num++;
if ($limit > 0 && $limit == $num) {
break;
}
}
return array_values($this->memcache->get($keys));
}
function makeEmpty()
{
$keys = $this->getAllKeys();
foreach ($keys as $value) {
$this->delete($value);
}
$this->delete("real");
$this->delete("front");
$this->delete("size");
$this->delete("maxSize");
}
private function getAllKeys()
{
if ($this->isEmpty())
{
return array();
}
$keys[] = $this->getKeyByPos($this->front);
for ($pos = ($this->front + 1) % $this->maxSize; $pos != $this->real; $pos = ($pos + 1) % $this->maxSize)
{
$keys[] = $this->getKeyByPos($pos);
}
return $keys;
}
private function add($pos, $data)
{
$this->memcache->add($this->getKeyByPos($pos), $data);
return $this;
}
private function increment($pos)
{
return $this->memcache->increment($this->getKeyByPos($pos));
}
private function decrement($pos)
{
$this->memcache->decrement($this->getKeyByPos($pos));
}
private function set($pos, $data)
{
$this->memcache->set($this->getKeyByPos($pos), $data);
return $this;
}
private function get($pos)
{
return $this->memcache->get($this->getKeyByPos($pos));
}
private function delete($pos)
{
return $this->memcache->delete($this->getKeyByPos($pos));
}
private function getKeyByPos($pos)
{
return $this->prefix . $this->name . $pos;
}
}

php  array_push 向数组增加值函数
 public static function insert(&$array, $key, $newValue, $before = true) {
  $result = false;
  $size = sizeof($array);
  for ($i=0; $i<$size; $i++) {
   $value = array_shift($array);
   if ($i==$key) {
    if ($before) {
     array_push($array, $newValue);
     array_push($array, $value);
    } else {
     array_push($array, $value);
     array_push($array, $newValue);
    }
    $result = true;
   } else {
    array_push($array, $value);
   }
  }
  if (!$result) {
   array_push($array, $newValue);
  }
  return;
 }


<?php
/**
* 进行写锁定的测试
* 打开线程1
*/
require("file_lock.php");
$lock = new File_Lock(dirname(dirname(__FILE__)) . "/FileLock.lock");
/** 单个线程锁定的速度 1s 钟 3万次。 **/
/** 两个线程写,两万的数据 大概要 7s 钟*/
/** 一个线程写,一万的数据 大概要 3.9s 钟,居然两个文件同时写,要快一点*/
/** 不进行锁定,一个进程 写大概要 2.8s 钟,加锁是有代价的。 */
/** 不进行锁定,两个进程 分布不是很均匀,而且大多数都冲突 */
$lock->writeLock();
$lock->increment();
$lock->unlock();
while ($lock->get() < 2) {
usleep(1000);
}
sleep(1);
echo "begin to runing n";
$t1 = microtime(true);
for ($i = 0; $i < 10000; $i++)
{
$lock->writeLock();
$lock->increment(1);
$lock->unlock();
}
$t2 = microtime(true) - $t1;
echo $t2;
?>


<?php
class File_Lock
{
private $name;
private $handle;
private $mode;
function __construct($filename, $mode = 'a+b')
{
global $php_errormsg;
$this->name = $filename;
$path = dirname($this->name);
if ($path == '.' || !is_dir($path)) {
global $config_file_lock_path;
$this->name = str_replace(array("/", "\"), array("_", "_"), $this->name);
if ($config_file_lock_path == null) {
$this->name = dirname(__FILE__) . "/lock/" . $this->name;
} else {
$this->name = $config_file_lock_path . "/" . $this->name;
}
}
$this->mode = $mode;
$this->handle = @fopen($this->name, $mode);
if ($this->handle == false) {
throw new Exception($php_errormsg);
}
}
public function close()
{
if ($this->handle !== null ) {
@fclose($this->handle);
$this->handle = null;
}
}
public function __destruct()
{
$this->close();
}
public function lock($lockType, $nonBlockingLock = false)
{
if ($nonBlockingLock) {
return flock($this->handle, $lockType | LOCK_NB);
} else {
return flock($this->handle, $lockType);
}
}
public function readLock()
{
return $this->lock(LOCK_SH);
}
public function writeLock($wait = 0.1)
{
$startTime = microtime(true);
$canWrite = false;
do {
$canWrite = flock($this->handle, LOCK_EX);
if(!$canWrite) {
usleep(rand(10, 1000));
}
} while ((!$canWrite) && ((microtime(true) - $startTime) < $wait));
}
/**
* if you want't to log the number under multi-thread system,
* please open the lock, use a+ mod. then fopen the file will not
* destroy the data.
*
* this function increment a delt value , and save to the file.
*
* @param int $delt
* @return int
*/
public function increment($delt = 1)
{
$n = $this->get();
$n += $delt;
$this->set($n);
return $n;
}
public function get()
{
fseek($this->handle, 0);
return (int)fgets($this->handle);
}
public function set($value)
{
ftruncate($this->handle, 0);
return fwrite($this->handle, (string)$value);
}
public function unlock()
{
if ($this->handle !== null ) {
return flock($this->handle, LOCK_UN);
} else {
return true;
}
}
}
?>

 

php  google 风格分页代码
public function showCtrlPanel_g($halfPer = 5) {
  
  $re = '<div class="pageMore">
   <ul>
    <li><span>'.$this->lineCount.'条</span></li>
    <li><span>'.$this->currentPage.'/'.$this->pageCount.'页</span></li>';
  if($this->currentPage-$halfPer >1){
   $re .= '<li><a href="'.$this->fileName.'pageno=1"><span>1</span></a></li>';
   if($this->currentPage-$halfPer*2 >1){
    $re .= '<li><a href="'.$this->fileName.'pageno='.($this->currentPage-$halfPer*2).'"><span>...</span></a></li>';
   }else{
    $re .= '<li><a href="'.$this->fileName.'pageno=1"><span>...</span></a></li>';
   }
  }
  for ( $i = $this->currentPage - $halfPer,$i > 1 || $i = 1 , $j = $this->currentPage + $halfPer, $j < $this->pageCount || $j = $this->pageCount;$i <= $j ;$i++ )
  {
   $re .= $i ==  $this->currentPage
    ? '<li class="linkOn"><a href="'.$this->fileName.'pageno='.$i.'"><span>'.$i.'</span></a></li>'." "
    : '<li><a href="'.$this->fileName.'pageno='.$i.'"><span>'.$i.'</span></a></li>'." ";
  }
  if($this->currentPage+$halfPer < $this->pageCount){
   if($this->currentPage+$halfPer*2 < $this->pageCount){
    $re .= '<li><a href="'.$this->fileName.'pageno='.($this->currentPage+$halfPer*2).'"><span>...</span></a></li>';
   }else{
    $re .= '<li><a href="'.$this->fileName.'pageno='.$this->pageCount.'"><span>...</span></a></li>';
   }
   $re .= '<li><a href="'.$this->fileName.'pageno='.$this->pageCount.'"><span>'.$this->pageCount.'</span></a></li>';
  }
    
  $re .= ' 
   </ul>
  </div>';
  return $re;
 }

php 日期转换成日时截

private function toTimeStamp ($dateTimeString = NULL) {
  if (!$dateTimeString) {
   $dateTimeString = time();
  }
  $numeric = '';
  $add_space = false;
  for($i=0;$i<strlen($dateTimeString);$i++) {
   if(strpos('0123456789',$dateTimeString[$i])===false) {
    if($add_space) {
     $numeric .= ' ';
     $add_space = false;
    }
   } else {
    $numeric .= $dateTimeString[$i];
    $add_space = true;
   }
  }
  
  $numeric_array = explode(' ',$numeric,6);
  if(sizeof($numeric_array)<3 || ($numeric_array[0]==0 && $numeric_array[1]==0 && $numeric_array[2]==0)) {
   throw new Exception($dateTimeString . ' is an invalid parameter', 5);
  } else {
   $result = mktime(intval($numeric_array[3]), intval($numeric_array[4]), intval($numeric_array[5]),
        intval($numeric_array[1]), intval($numeric_array[2]), intval($numeric_array[0])) ;
  }
  
  return $result;
 
 }

[!--infotagslink--]

相关文章

  • C#微信开发之发送模板消息

    这篇文章主要为大家详细介绍了C#微信开发之发送模板消息的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • SpringBoot集成Redis实现消息队列的方法

    这篇文章主要介绍了SpringBoot集成Redis实现消息队列的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-10
  • PHP分布式框架如何使用Memcache同步SESSION教程

    本教程主要讲解PHP项目如何用实现memcache分布式,配置使用memcache存储session数据,以及memcache的SESSION数据如何同步。 至于Memcache的安装配置,我们就不讲了,以前...2016-11-25
  • WM_CLOSE、WM_DESTROY、WM_QUIT及各种消息投递函数详解

    这篇文章主要介绍了WM_CLOSE、WM_DESTROY、WM_QUIT及各种消息投递函数,有助于读者更好的理解windows程序的消息机制,需要的朋友可以参考下...2020-04-25
  • PHP+memcache实现消息队列案例分享

    memche消息队列的原理就是在key上做文章,用以做一个连续的数字加上前缀记录序列化以后消息或者日志。然后通过定时程序将内容落地到文件或者数据库。php实现消息队列的用处比如在做发送邮件时发送大量邮件很费时间的问...2014-05-31
  • asp.net通过消息队列处理高并发请求(以抢小米手机为例)

    这篇文章主要介绍了asp.net通过消息队列处理高并发请求(以抢小米手机为例),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
  • 简单用VBS调用企业微信机器人发定时消息的方法

    这篇文章主要介绍了简单用VBS调用企业微信机器人发定时消息的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08
  • python实现企业微信定时发送文本消息的实例代码

    这篇文章主要介绍了python实现企业微信定时发送文本消息的实例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-25
  • 基于条件变量的消息队列 说明介绍

    本篇文章小编为大家介绍,基于条件变量的消息队列 说明介绍。需要的朋友参考一下...2020-04-25
  • jquery插件jquery.confirm弹出确认消息

    这篇文章介绍了插件jquery.confirm弹出确认消息的实现方法,感兴趣的小伙伴们可以参考一下...2015-12-24
  • Windows窗口消息实例详解

    这篇文章主要介绍了Windows窗口消息,以实例形式详细罗列了Windows窗口消息,非常具有实用价值,需要的朋友可以参考下...2020-04-25
  • 进程间通信之深入消息队列的详解

    本篇文章是对消息队列的应用进行了详细的分析介绍,需要的朋友参考下...2020-04-25
  • 帝国CMS会员注册字段增加编辑器、发送短消息改为编辑框

    通过本教程可以实现帝国CMS后台给前台注册用户发消息,把内容输入框改为编辑器,可上传图片,等打开文件\e\admin\member\SendMsg.php 大约84行<textarea name="msgtext" cols="6...2016-01-27
  • Windows下Memcache的安装方法

    很多phper不知道如何在Windows下搭建Memcache的开发调试环境,最近个人也在研究Memcache,记录下自己安装搭建的过程。 ...2016-01-27
  • 在Mac OS的PHP环境下安装配置MemCache的全过程解析

    这篇文章主要介绍了在Mac OS的PHP环境下安装配置MemCache的全过程解析,MemCache是一套分布式的高速缓存系统,需要的朋友可以参考下...2016-02-18
  • 微信自动回复消息

    在微信回复信息,POST的XML数据如下图所示 ToUserName 为接收者名称 FromUserName 为发送者名称 被动回复,响应的信息如下: 这里要注意,ToUserName和FromU...2016-05-19
  • php memcache和php memcached比较以及问题

    php memcache和php memcached是php的memcache分布式的高速缓存系统的两个客户端,php memcache是老客户端,php memcached是功能更加完善的新的代替php memcached的。...2016-11-25
  • Go语言使用钉钉机器人推送消息的实现示例

    本文主要介绍了Go语言使用钉钉机器人推送消息的实现示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-23
  • Delphi实现获取句柄并发送消息的方法

    这篇文章主要介绍了Delphi实现获取句柄并发送消息的方法,需要的朋友可以参考下...2020-06-30
  • php memcache和memcached模块安装应用

    memcache的官方主页:php教程.net/package/memcache">http://pecl.php.net/package/memcache memcached的官方主页:http://pecl.php.net/package/memcached 以下是我安装...2016-11-25