Heim > Backend-Entwicklung > PHP-Tutorial > einfache Dateiklasse

einfache Dateiklasse

WBOY
Freigeben: 2016-08-08 09:30:23
Original
936 Leute haben es durchsucht

<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>
Nach dem Login kopieren

Das Obige stellt einfache Dateiklassen vor, einschließlich inhaltlicher Aspekte. Ich hoffe, dass es für Freunde hilfreich ist, die sich für PHP-Tutorials interessieren.

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage