12
A
回答
11
使用http://php.net/manual/en/function.serialize.php
<?php
// connect memcache
$memcache_obj = new Memcache;
$memcache_obj->connect('localhost', 11211);
// simple example class
class MyClass {
private $var = 'default';
public function __construct($var = null) {
if ($var) {
$this->setVar($var);
}
}
public function getVar() {
return $this->var;
}
public function setVar($var) {
$this->var = $var;
}
}
$obj1 = new MyClass();
$obj2 = new MyClass('test2');
$obj3 = new MyClass();
$obj3->setVar('test3');
// dump the values using the method getVar
var_dump($obj1->getVar(), $obj2->getVar(), $obj3->getVar());
// store objects serialized in memcache, set MEMCACHE_COMPRESSED as flag so it takes less space in memory
$memcache_obj->set('key1', serialize($obj1), MEMCACHE_COMPRESSED);
$memcache_obj->set('key2', serialize($obj2), MEMCACHE_COMPRESSED);
$memcache_obj->set('key3', serialize($obj3), MEMCACHE_COMPRESSED);
// unset the objects to prove it ;-)
unset($obj1, $obj2, $obj3);
// get the objects from memcache and unserialze
// IMPORTANT: THE CLASS NEEEDS TO EXISTS!
// So if you have MyClass in some other file and include it, it has to be included at this point
// If you have an autoloader then it will work easily ofcourse :-)
$obj1 = unserialize($memcache_obj->get('key1'));
$obj2 = unserialize($memcache_obj->get('key2'));
$obj3 = unserialize($memcache_obj->get('key3'));
// dump the values using the method getVar
var_dump($obj1->getVar(), $obj2->getVar(), $obj3->getVar());
?>
相关问题
- 1. 将复杂对象存储在redis或memcached中(ruby)
- 2. 如何将复杂的Perl对象存储到Memcached中?
- 3. 散装存储复杂对象的SQLAlchemy
- 4. 在iOS中存储复杂对象?
- 5. Object.assign()具复杂对象Angular2 NGRX /存储
- 6. 复杂对象的存储库设计?
- 7. 在web.config中存储复杂的对象
- 8. 绕过复杂查询的存储库模式可以吗?
- 9. 可以使用对象存储GE来存储图像吗?
- 10. Azure存储 - NodeJS - 我可以存储对象吗?
- 11. 加载复杂对象以存储sencha触摸1.1
- 12. 通过存储过程存储复杂对象
- 13. memcached的在Rails的对象存储
- 14. 每个对象的Memcached存储
- 15. 如何在memcached中存储protobuf对象?
- 16. 我们可以在Kendo Grid中读取复杂对象吗?
- 17. RestEasy:可以发布两个复杂对象吗?
- 18. 可以在http CreateErrorResponse中返回复杂的对象内容吗?
- 19. 我可以在对象中存储对变量的引用吗?
- 20. 在SQLite中存储对象。可能吗?
- 21. 重复的memcached对象
- 22. 复杂对象
- 23. 对复杂对象
- 24. 只有MySqlParameter对象可以存储(C#)
- 25. Google appengine可以存储Java对象POJO
- 26. iOS:我可以在托管中存储托管对象吗?
- 27. 我们可以在Blob中存储org.apache.tomcat.websocket.WsSession对象吗?
- 28. 可以强制将对象存储在堆中吗?
- 29. 我可以将JavaScript对象存储在mySQL数据库中吗?
- 30. 我可以在ViewState中存储xmlDocument对象吗?
只有当他们系列化;并且该类必须已被包含/定义,然后才能再次反序列化 –
如果要序列化闭包,则需要类似https://github.com/jeremeamia/super_closure –