我有一个映射到数据库行的用户类。我将它缓存在memcached中,作为使用userid作为memcached密钥的键值对。我想将所有用户功能封装到用户类中,包括填充用户类字段。在从PDO中获取数据时,我使用PDO :: FETCH_INTO将值存储在自我对象中。如何使用memcached执行此操作?从memcached获取对象并在PHP中设置为self
-1
A
回答
2
你问题的措辞和跟进的意见是有些模糊,但他们仍然指向我在以下方向:
public function __construct($id) {
global $pdo, $memcached;
$data = $memcached->get($id);
if($memcached->getResultCode() == Memcached::RES_SUCCESS) {
// this is not currently allowed in PHP
$this = $data;
// this should be your fix
foreach($data AS $key => $value) {
$this->$key = $value;
}
// or this
foreach($this AS $key => $value) {
$this->$key = $data[$key];
}
// the difference between the fixes above is that
// the second is strictly limited to values defined
// by the class (current object)
}
else {
$pdos = $pdo->prepare('SELECT * FROM table_name WHERE id = ?');
if($pdos) {
// this is not allowed in PHP
$pdos->execute(array(intval($id)));
$this = $pdos->fetch(PDO::FETCH_CLASS, get_class($this));
// all of this should work fine and is allowed
$pdos->setFetchMode(PDO::FETCH_INTO, $this);
$pdos->execute(array(intval($id)));
$pdos->fetch(PDO::FETCH_INTO);
}
}
}
但不幸的是PHP不允许覆盖的$该值在内部(它自己的内部方法调用),因此可以作为替代方法使用静态方法。
public static function getByID($id) {
global $pdo, $memcached;
$data = $memcached->get($id);
if($memcached->getResultCode() == Memcached::RES_SUCCESS) {
// this will work if your objects construct has a
// foreach similar to the ones presented above
$result = new self($data);
// or if you don't want to write a foreach in
// the construct you can have it here
foreach($data AS $key => $value) {
$this->$key = $value;
}
// or here
foreach($this AS $key => $value) {
$this->$key = $data[$key];
}
}
else {
$pdos = $pdo->prepare('SELECT * FROM table_name WHERE id = ?');
if($pdos) {
// either of these should work
$pdos->execute(array(intval($id)));
$result = $pdos->fetch(PDO::FETCH_CLASS, get_class($this));
// either of these should work
$result = new self;
$pdos->setFetchMode(PDO::FETCH_INTO, $result);
$pdos->execute(array(intval($id)));
$pdos->fetch(PDO::FETCH_INTO);
}
}
return($result);
}
使用语法是MyClass::get($some_id)
。
+0
感谢您的回答。我们昨天刚刚实施了类似的东西:) – mrd081 2012-08-15 05:37:05
1
答案要么是“只管去做”或者“你不这样做”。如果你把你的信息单独保存为键/值,你不能只用一次命中就完成,你只需要手工检索它们(使用memcached中的计算键创建一个新对象)。
如果你在分布式缓存序列化的对象,你可以检索和反序列化。
+0
问题是从memcached返回的对象中为self对象分配值。 – mrd081 2012-08-14 10:49:56
相关问题
- 1. 从XML中加载用户设置并将其设置为对象。未将对象设置为对象实例
- 2. 在AngularJS中获取并设置一个工厂值/对象
- 3. 在C++中创建一个对象并获取 - 设置
- 4. 在PHP中设置Memcached常量
- 5. 从Memcached中获取对象的调用方法
- 6. 获取对象的类型并将其动态设置为对象
- 7. 如何获取Firebase对象并将其设置为等于本地对象?
- 8. 为什么在.h中将UI对象委托设置为self时声明?
- 9. 如何从arraylist中获取对象并在文本视图上设置
- 10. jQuery在多维对象中获取的值设置为true
- 11. 获取对象引用未设置为从静态成员
- 12. 从PHP中的对象获取属性
- 13. PHP:从对象中获取数据
- 14. 合并两个对象数组并获取唯一对象PHP
- 15. 设置self.delegate = self
- 16. 设置和获取对象从存储在离子2
- 17. 配置节设计工具 - 从对象中获取XML
- 18. Excel对象不获取设置有时
- 19. 动态设置和获取对象
- 20. Java异常抛出对象并从catch中获取对象
- 21. 从memcached获取数据
- 22. 从数组中提取数据并将其设置在java中的对象中
- 23. PHP - 将数组设置为对象
- 24. 获取并设置对象的项目 - chrome.storage
- 25. 如何获取并设置Spring的Autowired对象?
- 26. 从其他对象中获取对象
- 27. print_r在PHP中获取对象方法?
- 28. 在PHP中获取Objective-C对象
- 29. 在PHP中获取对象相关性
- 30. 从数据库中获取数据并将其设置为edittext
我不明白这个问题,你卡在哪里? – 2012-08-13 09:12:45
问题是从memcached返回的对象中为self对象分配值。 – mrd081 2012-08-14 10:49:11