首頁 > 後端開發 > php教程 > 簡單的文件類

簡單的文件類

WBOY
發布: 2016-08-08 09:30:23
原創
919 人瀏覽過

<span> 1</span> <?<span>php
</span><span> 2</span>     <span>/*</span><span>*
</span><span> 3</span> <span>     * @desc 文件缓存
</span><span> 4</span>      <span>*/</span>
<span> 5</span>     <span>class</span><span> Cache{
</span><span> 6</span>         <span>const</span> C_FILE = '/Runtime/'<span>;
</span><span> 7</span>         <span>private</span> <span>$dir</span> = ''<span>;
</span><span> 8</span>         <span>const</span> EXT = '.tpl'<span>;
</span><span> 9</span>         <span>private</span> <span>$filename</span> = ''<span>;
</span><span>10</span>         <span>public</span> <span>function</span> __construct(<span>$dir</span> = ''<span>){
</span><span>11</span>             <span>$this</span>-><span>dir</span> = <span>$dir</span><span>;
</span><span>12</span>             
<span>13</span> <span>        }
</span><span>14</span>         <span>/*</span><span>*
</span><span>15</span> <span>         *    @desc 设置文件缓存
</span><span>16</span> <span>         *    @param string $key 文件名 
</span><span>17</span> <span>         *    @param unkonw $data 缓存数据
</span><span>18</span> <span>         *  @param int    $expire 过期时间
</span><span>19</span>          <span>*/</span>
<span>20</span>         <span>public</span> <span>function</span> set(<span>$key</span>,<span>$data</span>,<span>$expire</span> = 0<span>){
</span><span>21</span>             <span>$this</span>->filename =     <span>dirname</span>(<span>__FILE__</span>).self::C_FILE.<span>$this</span>-><span>dir</span>.<span>$key</span>.self::<span>EXT;
</span><span>22</span>             <span>if</span>(<span>file_exists</span>(<span>$this</span>-><span>filename)){
</span><span>23</span>                 <span>$res</span> = <span>$this</span>->get(<span>$key</span><span>);
</span><span>24</span>                 <span>if</span>(<span>md5</span>(<span>$res</span>) == <span>md5</span>(json_encode(<span>$data</span><span>) ) ){
</span><span>25</span>                     <span>return</span> <span>true</span><span>;
</span><span>26</span> <span>                }
</span><span>27</span> <span>            }
</span><span>28</span>             <span>if</span>(!<span>is_dir</span>(<span>dirname</span>(<span>$this</span>-><span>filename))){
</span><span>29</span>                 <span>mkdir</span>(<span>dirname</span>(<span>$this</span>->filename),0777<span>);
</span><span>30</span> <span>            }
</span><span>31</span>             
<span>32</span>             <span>$source</span> = <span>fopen</span>(<span>$this</span>->filename,'w+'<span>);
</span><span>33</span>             <span>fwrite</span>(<span>$source</span>,json_encode(<span>$data</span><span>));
</span><span>34</span>             <span>fclose</span>(<span>$source</span><span>);
</span><span>35</span> <span>        }
</span><span>36</span>         
<span>37</span>         <span>/*</span><span>*
</span><span>38</span> <span>         *    @desc 获取文件
</span><span>39</span> <span>         *  @param string $key 文件名
</span><span>40</span>          <span>*/</span>
<span>41</span>         <span>public</span> <span>function</span> get(<span>$key</span><span>){
</span><span>42</span>             <span>//</span><span>$filename = dirname(__FILE__).self::C_FILE.$this->dir.$key.self::EXT;</span>
<span>43</span>             <span>if</span>(!<span>file_exists</span>(<span>$this</span>-><span>filename)){
</span><span>44</span>                 <span>return</span> '缓存文件已经不存在'<span>;
</span><span>45</span>             }<span>else</span><span>{
</span><span>46</span>                 <span>$res</span> = <span>file_get_contents</span>(<span>$this</span>-><span>filename);
</span><span>47</span> <span>            }
</span><span>48</span>             <span>return</span> <span>$res</span><span>;
</span><span>49</span> <span>        }
</span><span>50</span>         <span>/*</span><span>*
</span><span>51</span> <span>         *    @desc 删除文件
</span><span>52</span> <span>         *  @param string $key 文件名
</span><span>53</span>          <span>*/</span>
<span>54</span>         <span>public</span> <span>function</span> del(<span>$key</span><span>){
</span><span>55</span>             <span>unlink</span>(<span>$this</span>-><span>filename);
</span><span>56</span> <span>        }
</span><span>57</span>         
<span>58</span> <span>    }
</span><span>59</span>     
<span>60</span>     <span>$data</span> = <span>array</span>('name'=>'song','age'=>20,'sex'=>'man','favority'=><span>array</span>('apple','banana'<span>));
</span><span>61</span>     <span>$cache</span> = <span>new</span><span> Cache();
</span><span>62</span>     <span>$cache</span>->set('cache',<span>$data</span><span>);
</span><span>63</span>     <span>//</span><span>$cache->get('cache');
</span><span>64</span> <span>    //$cache->del('cache');</span>
登入後複製

以上就介紹了簡單的文件類,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板