VariableService.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * 系统变量sevice
  4. * 使用示例:
  5. * $result = Service::factory('VariableService')->getVariable('test');
  6. */
  7. class VariableService extends Service
  8. {
  9. public function getVariable($key){
  10. $Key = HelperKey::generateRedisKey($key);
  11. $result = VariableRedis::get($Key);
  12. if(empty($result)){
  13. $criteria = new EMongoCriteria();
  14. $criteria->_id('==', $key);
  15. $variable = Variable::model()->find($criteria);
  16. if($variable){
  17. $value = $variable->value;
  18. VariableRedis::set($Key,$value);
  19. return $value;
  20. }else{
  21. return null;
  22. }
  23. }else{
  24. return $result;
  25. }
  26. }
  27. public function setVariable($key,$value){
  28. $criteria = new EMongoCriteria();
  29. $criteria->_id('==', $key);
  30. $variableModel = Variable::model()->find($criteria);
  31. if(!$variableModel){
  32. if($this->addVariable($key,$value)){
  33. $Key = HelperKey::generateRedisKey($key);
  34. return VariableRedis::set($Key,$value);
  35. }else{
  36. return false;
  37. }
  38. }else{
  39. $variableModel->value = $value;
  40. if($variableModel->update(array('value'),true)){
  41. $Key = HelperKey::generateRedisKey($key);
  42. return VariableRedis::set($Key,$value);
  43. }else{
  44. return false;
  45. }
  46. }
  47. }
  48. public function delVariable($key){
  49. $criteria = new EMongoCriteria();
  50. $criteria->_id('==', $key);
  51. $variableModel = Variable::model()->find($criteria);
  52. $Key = HelperKey::generateRedisKey($key);
  53. if (VariableRedis::remove($Key)&&$variableModel->delete()) {
  54. return true;
  55. }
  56. return false;
  57. }
  58. private function addVariable($key,$value){
  59. $variableModel = new Variable();
  60. $variableModel->_id = $key;
  61. $variableModel->value = $value;
  62. if ($variableModel->save()){
  63. return true;
  64. }else{
  65. return false;
  66. }
  67. }
  68. }
  69. ?>