VariableRedis.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * VariableRedis 常用变量
  4. * 用法:
  5. * //首先得到key,(可以为多个参数)
  6. * $Key = HelperKey::generateRedisKey($variable_name);
  7. * // get
  8. * $indexSlideData = VariableRedis::get($Key);
  9. * // set
  10. * $rs = VariableRedis::set($Key, $data);
  11. * $Id$
  12. */
  13. class VariableRedis extends RedisAr{
  14. /**
  15. * _prefix
  16. * 前缀
  17. *
  18. * @var mixed
  19. */
  20. public static $_prefix = 'variable_';
  21. /**
  22. * get
  23. *
  24. * @param mixed $key
  25. * @return void
  26. */
  27. public static function get($key) {
  28. try {
  29. return Yii::app()->redis->getClient()->get($key);
  30. } catch (Exception $e) {
  31. throw new CException(Yii::t('model', $e->getMessage()));
  32. }
  33. }
  34. /**
  35. * set
  36. *
  37. * @param string $data
  38. * @return void
  39. * @param $time 在redis里的有效时间
  40. */
  41. public static function set($key, $data, $time = 86400) {
  42. try {
  43. $return = Yii::app()->redis->getClient()->set($key, $data);
  44. if($time != null){
  45. return Yii::app()->redis->getClient()->expire($key, $time);
  46. }else{
  47. return $return;
  48. }
  49. } catch (Exception $e) {
  50. throw new CException(Yii::t('model', $e->getMessage()));
  51. }
  52. }
  53. public static function remove($key) {
  54. try {
  55. Yii::app()->redis->getClient()->del($key);
  56. return true;
  57. } catch (Exception $e) {
  58. throw new CException(Yii::t('model', $e->getMessage()));
  59. }
  60. }
  61. }