ShareMemory

ShareMemory是为了解决多进程下进程数据隔离的问题。基于文件+锁的形式来实现。

方法列表

getInstance

用于获取一个ShareMemory实例。

  1. use \Core\Component\ShareMemory;
  2. $share = ShareMemory::getInstance();
  3. //或者是
  4. $share = ShareMemory::getInstance(ShareMemory::SERIALIZE_TYPE_SERIALIZE);

对数据的存储,EasySwoole默认选用json_encode的方式,若需要存储对象,请选SERIALIZE_TYPE_SERIALIZE。

set && get

  1. $share->set("a",2);
  2. $share->set('a',$share->get("a"));
  3. var_dump($share->get("a"));
  4. $share->set("b.c","aaaa");
  5. echo $share->get("b.c");

startTransaction && commit && rollback

  1. $share = \Core\Component\ShareMemory::getInstance(\Core\Component\ShareMemory::SERIALIZE_TYPE_SERIALIZE);
  2. $share->startTransaction();
  3. $share->set("a",2);
  4. $share->set('a',$share->get("a"));
  5. var_dump($share->get("a"));
  6. $share->commit();
  7. var_dump($share->get("a"));
  8. $share->set("b.c","aaaa");
  9. echo $share->get("b.c");

事务必须提交,否则会引起死锁。

  1. $share->startTransaction();
  2. $share->set("a",2);
  3. $share->set('a',$share->get("a"));
  4. $share->rollback();
  5. var_dump($share->get("a"));
  6. $share->commit();
  7. var_dump($share->get("a"));
  8. $share->set("b.c","aaaa");
  9. echo $share->get("b.c");

clear

  1. $share->clear();

注意,该操作会清除全部的数据。