📄 正在查看:twcms/model/runtime_model.class.php
大小:2,521 字节 · 修改:2014-01-23 01:42:52 · 行数:97
1<?php
2/**
3 * (C)2012-2014 twcms.com TongWang Inc.
4 * Author: wuzhaohuan <kongphp@gmail.com>
5 */
6
7defined('TWCMS_PATH') or exit;
8
9class runtime extends model {
10 private $data = array(); // 保证唯一性
11 private $changed = array(); // 表示修改过的key
12
13 function __construct() {
14 $this->table = 'runtime'; // 表名
15 $this->pri = array('k'); // 主键
16
17 // hook runtime_model_construct_after.php
18 }
19
20 // 读取缓存
21 public function get($k) {
22 $arr = parent::get($k);
23 return !empty($arr) && (empty($arr['expiry']) || $arr['expiry'] > $_ENV['_time']) ? _json_decode($arr['v']) : array();
24 }
25
26 // 写入缓存
27 public function set($k, $s, $life = 0) {
28 $s = json_encode($s);
29 $arr = array();
30 $arr['k'] = $k;
31 $arr['v'] = $s;
32 $arr['expiry'] = $life ? $_ENV['_time'] + $life : 0;
33 return parent::set($k, $arr);
34 }
35
36 // 读取
37 public function xget($key = 'cfg') {
38 if(!isset($this->data[$key])) {
39 $this->data[$key] = $this->get($key);
40 if($key == 'cfg' && empty($this->data[$key])) {
41 $cfg = (array)$this->kv->get('cfg');
42
43 empty($cfg['theme']) && $cfg['theme'] = 'default';
44
45 $cfg['tpl'] = $cfg['webdir'].(defined('F_APP_NAME') ? F_APP_NAME : APP_NAME).'/view/'.$cfg['theme'].'/';
46 $cfg['webroot'] = 'http://'.$cfg['webdomain'];
47 $cfg['weburl'] = 'http://'.$cfg['webdomain'].$cfg['webdir'];
48
49 $table_arr = $this->models->get_table_arr();
50 $cfg['table_arr'] = $table_arr;
51
52 $mod_name = $this->models->get_name();
53 unset($mod_name[1]);
54 $cfg['mod_name'] = $mod_name;
55
56 $categorys = $this->category->get_category_db();
57 $cate_arr = array();
58 foreach($categorys as $row) {
59 $cate_arr[$row['cid']] = $row['alias'];
60 }
61 $cfg['cate_arr'] = $cate_arr;
62
63 $this->data[$key] = &$cfg;
64 $this->set('cfg', $this->data[$key]);
65 }
66 }
67 return $this->data[$key];
68 }
69
70 // 修改
71 public function xset($k, $v, $key = 'cfg') {
72 if(!isset($this->data[$key])) {
73 $this->data[$key] = $this->get($key);
74 }
75 if($v && is_string($v) && ($v[0] == '+' || $v[0] == '-')) {
76 $v = intval($v);
77 $this->data[$key][$k] += $v;
78 }else{
79 $this->data[$key][$k] = $v;
80 }
81 $this->changed[$key] = 1;
82 }
83
84 // 保存
85 public function xsave($key = 'cfg') {
86 $this->set($key, $this->data[$key]);
87 $this->changed[$key] = 0;
88 }
89
90 // 保存所有修改过的key
91 public function save_changed() {
92 foreach($this->changed as $key=>$v) {
93 $v && $this->xsave($key);
94 }
95 }
96}
97