PHP文件缓存类,_PHP教程

WBOY
Libérer: 2016-07-13 10:08:48
original
782 Les gens l'ont consulté

PHP文件缓存类,

<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>
Copier après la connexion

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/950253.htmlTechArticlePHP文件缓存类, 1 ? php 2 /* * 3 * @desc 文件缓存 4 */ 5 class Cache{ 6 const C_FILE = '/Runtime/' ; 7 private $dir = '' ; 8 const EXT = '.tpl' ; 9 private $filename = ''...
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!