我有一個映射到數據庫行的用戶類。我將它緩存在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. 從HttpSessions(Java)設置/獲取對象
- 3. 在C++中創建一個對象並獲取 - 設置
- 4. 在AngularJS中獲取並設置一個工廠值/對象
- 5. 在Java中與Memcached共享PHP對象
- 6. 在PHP中設置Memcached常量
- 7. php從url獲取對象
- 8. 從Memcached中獲取對象的調用方法
- 9. 從mySQL獲取ID並使用php在ajax中設置localstorage
- 10. 獲取對象的類型並將其動態設置爲對象
- 11. 如何獲取Firebase對象並將其設置爲等於本地對象?
- 12. 爲什麼在.h中將UI對象委託設置爲self時聲明?
- 13. 如何從arraylist中獲取對象並在文本視圖上設置
- 14. jQuery在多維對象中獲取的值設置爲true
- 15. 獲取未設置爲對象實例的對象引用
- 16. 用spymemcached設置的項目不能用php memcached獲取
- 17. 獲取對象引用未設置爲從靜態成員
- 18. PHP從類中獲取數組對象
- 19. PHP:從對象中獲取數據
- 20. 從PHP中的對象獲取屬性
- 21. 合併兩個對象數組並獲取唯一對象PHP
- 22. 在cellforrowatindexpath中將對象的代表設置爲nil,指定給self自我
- 23. 獲取並設置在C#
- 24. PHP - 設置並獲取方法
- 25. 取消設置mysql_fetch_field對象值MYSQL PHP
- 26. PHP類設計 - 獲取多個對象
- 27. Android:獲取用戶代理對象中設置的json對象
- 28. 設置self.delegate = self
- 29. 配置節設計工具 - 從對象中獲取XML
- 30. 設置和獲取對象從存儲在離子2
我不明白這個問題,你卡在哪裏? – 2012-08-13 09:12:45
問題是從memcached返回的對象中爲self對象分配值。 – mrd081 2012-08-14 10:49:11