Memcache 一般用于缓存服务。但是很多时候,比如一个消息广播系统,需要一个消息队列。直接从数据库取消息,负载往往不行。如果将整个消息队列用一个key缓存到memcache里面,

对于一个很大的消息队列,频繁进行进行大数据库的序列化 和 反序列化,有太耗费。下面是我用PHP 实现的一个消息队列,只需要在尾部插入一个数据,就操作尾部,不用操作整个消息队列进行读取,与操作。但是,这个消息队列不是线程安全的,我只是尽量的避免了冲突的可能性。如果消息不是非常的密集,比如几秒钟才一个,还是可以考虑这样使用的。

如果你要实现线程安全的,一个建议是通过文件进行锁定,然后进行操作。下面是代码:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->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;

<script type="text/javascript"><!-- google_ad_client = "pub-6770445892601887"; /* 468x60, 创建于 09-11-19 */ google_ad_slot = "4437639877"; google_ad_width = 468; google_ad_height = 60; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>         $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;
    }
}

Memcache 中实现消息队列相关推荐

  1. OSSIM中分布式消息队列应用

     OSSIM中分布式消息队列应用 1. 消息队列处理 企业日志数量正在以指数级形式高速增长,日志数据的具有海量.多样.异构等特点,基于传统的单一节点混合式安装的OSSIM平台(指OSSIM 4.4及以 ...

  2. 【Android 异步操作】手写 Handler ( Message 消息 | ThreadLocal 线程本地变量 | Looper 中的消息队列 MessageQueue )

    文章目录 一.Message 消息 二.ThreadLocal 线程本地变量 三.Looper 中的消息队列 MessageQueue 一.Message 消息 模仿 Android 中的 Messa ...

  3. python队列来做什么_python分布式爬虫中的消息队列是什么?

    当排队等待人数过多的时候,我们需要设置一个等待区防止秩序混乱,同时再有新来的想要排队也可以呆在这个地方.那么在python分布式爬虫中,消息队列就相当于这样的一个区域,爬虫要进入这个区域找寻自己想要的 ...

  4. GaussDB(DWS)中共享消息队列实现的三大功能

    摘要:本文将详细介绍GaussDB(DWS)中共享消息队列的实现. 本文分享自华为云社区<GaussDB(DWS)CBB组件之共享消息队列介绍>,作者:疯狂朔朔. 1)共享消息队列是什么? ...

  5. RTOS中的消息队列的原理以及应用

    消息队列的原理 RTOS中的消息队列是一种用于任务间通信的机制,它可以实现任务之间的异步通信,从而实现任务间的解耦.消息队列是一个先进先出的数据结构,任务可以向队列中发送消息,也可以从队列中接收消息. ...

  6. 智能家居DIY连载教程(2)——在实际项目中运用消息队列与邮箱

    前言 千呼万唤始出来,智能家居 DIY 教程连载第二篇终于登场了!本文将重点给大家介绍如何将消息队列与邮箱运用到实际项目中去.一起来看看吧~ DIY 回顾上期: 1.智能家居DIY连载教程(1)--如 ...

  7. 详解SpringCloud中RabbitMQ消息队列原理及配置,一篇就够!

    作者:kosamino cnblogs.com/jing99/p/11679426.html 一.MQ用途 1.同步变异步消息 场景:用户下单完成后,发送邮件和短信通知. 运用消息队列之后,用户下单完 ...

  8. C#中使用消息队列服务

    C#中使用Windows消息队列服务 http://www.cnblogs.com/xinhaijulan/archive/2010/08/22/1805768.html http://h2appy. ...

  9. win7系统中的消息队列服务器,高手分析win7系统安装消息队列的详细

    win7系统是计算机学员最喜欢使用的电脑系统,却有一些学员在操作过程中应该会面临对win7系统安装消息队列进行布置的情景.几乎所有的初学者计算机知识还很薄弱,关于win7系统安装消息队列的情况,仍旧一 ...

最新文章

  1. 人工智能与智能系统的先驱人物
  2. 企业跟风移动考勤市场,变革下的移动考勤应该怎么玩?
  3. 【转】日服巫术online过驱动保护分析(纯工具)(工具+自写驱动)
  4. [导入]Google Earth坐标集(能更看清这个世界喽!)
  5. 第五十三期:公司如何选择数据库?DynamoDB、Hadoop和MongoDB 大比拼
  6. JAVA入门级教学之(for循环)
  7. Java基础知识之循环语句(for循环、while循环)
  8. 爬虫进阶 -- 爬虫相关定义、反爬机制及其破解
  9. 规则引擎 drools_Drools的入门初探
  10. 期末考试之排名次java_2020超星尔雅《JavaWeb应用开发》期末测试答案
  11. guice android,android – 如何使用Guice的@Singleton?
  12. 关于电的计算机公式,电能计算-电能的计算公式-电工基础 - 电工屋
  13. 图片怎么转文字?建议收藏这些方法
  14. 全面公测 | 百度智能云CCE在离线混部功能
  15. webgis、gis学习技巧总结
  16. 红与黑 DFS(JAVA解法)
  17. ubuntu 或者虚拟机连接u盘
  18. 卡内基·梅隆大学计算机科学系主任周以真的父母是中国人吗,计算思维(Computational Thinking)...
  19. 河北计算机软件职业技术学院,河北软件职业技术学院
  20. 手机2020 QQ 群文件下载存储路径

热门文章

  1. 信息学奥赛一本通(1127:图像旋转)
  2. 小朋友(洛谷-P3852)
  3. 图论 —— 最短路 —— Dijkstra 算法
  4. 17 PP配置-生产计划-总体维护工厂参数
  5. 4 CO配置-企业结构-分配-把控制范围分配给经营范围
  6. php怎么选择路径,利用php+mcDropdown实现文件路径可在下拉框选择
  7. 学生管理系统服务器端设计,学生信息管理系统设计与实现
  8. 【Transformer】TransGAN的鉴别器的理解
  9. 【pytorch】torch.range() 和 torch.arange() ==>以step为间隔输出从start到end的张量列表
  10. hls二次加密 m3u8_将视频转换为m3u8,使用AES-128的方式加密HLS真的有效吗?