Rumah > php教程 > php手册 > 报表的缓存基本存储和读写,报表缓存读写

报表的缓存基本存储和读写,报表缓存读写

WBOY
Lepaskan: 2016-06-13 09:05:01
asal
1057 orang telah melayarinya

报表的缓存基本存储和读写,报表缓存读写

<?<span>php
</span><span>/*</span><span>*
 * Class Rpt redis 用于报表的缓存基本存储和读写 2.0
 * @simple
 * Rpt::read("diamond.account",$nick);
 * Rpt::readSync("diamond.account",$nick);
 * $finder = Rpt::createFinder("diamond.account",$nick);
 * $finder->read();
 * $finder->readSync();
 *
 * Rpt::save("diamond.account",$nick,$data);
 * $storage = Rpt::createStorage("diamond.account",$nick);
 * $storage->save($data);
 * $storage->save($data,7200);
 </span><span>*/</span>
<span>class</span><span> Rpt {
    </span><span>public</span> <span>static</span> <span>function</span> read(<span>$key</span>,<span>$nick</span><span>){
        </span><span>$finder</span> = self::createFinder(<span>$key</span>,<span>$nick</span><span>);
        </span><span>return</span> <span>$finder</span>-><span>read();
    }

    </span><span>public</span> <span>static</span> <span>function</span> readSync(<span>$key</span>,<span>$nick</span><span>){
        </span><span>$finder</span> = self::createFinder(<span>$key</span>,<span>$nick</span><span>);
        </span><span>return</span> <span>$finder</span>-><span>readSync();
    }

    </span><span>public</span> <span>static</span> <span>function</span> createFinder(<span>$key</span>,<span>$nick</span><span>){
        </span><span>$key</span> = RptGenerate::<span>key</span>(<span>$key</span>,<span>$nick</span><span>);
        </span><span>return</span> <span>new</span> RptFinder(<span>$key</span><span>);
    }

    </span><span>public</span> <span>static</span> <span>function</span> createStorage(<span>$key</span>,<span>$nick</span><span>){
        </span><span>$key</span> = RptGenerate::<span>key</span>(<span>$key</span>,<span>$nick</span><span>);
        </span><span>return</span> <span>new</span> RptStorage(<span>$key</span><span>);
    }

    </span><span>public</span> <span>static</span> <span>function</span> save(<span>$key</span>,<span>$nick</span>,<span>$data</span>,<span>$expired</span>=7200<span>){
        </span><span>$storage</span> = self::createStorage(<span>$key</span>,<span>$nick</span><span>);
        </span><span>return</span> <span>$storage</span>->save(<span>$data</span>,<span>$expired</span><span>);
    }


    </span><span>public</span> <span>static</span> <span>function</span><span> createRedis(){
        </span><span>$redis</span> = <span>new</span><span> Redis();
        </span><span>$redis</span>->connect(Yii::app()->params["RedisServerIP"<span>]);
        </span><span>return</span> <span>$redis</span><span>;
    }

}

</span><span>/*</span><span>*
 * Class RptFinder  数据读取
 </span><span>*/</span>
<span>class</span><span> RptFinder {

    </span><span>/*</span><span>*
     * @var string $key
     </span><span>*/</span>
    <span>public</span> <span>$key</span><span>;

    </span><span>/*</span><span>*
     * @param string $key
     </span><span>*/</span>
    <span>public</span> <span>function</span> __construct(<span>$key</span><span>){
        </span><span>$this</span>-><span>key</span> = <span>$key</span><span>;
    }

    </span><span>/*</span><span>*
     * 非安全读取数据
     * @return mixed
     </span><span>*/</span>
    <span>public</span> <span>function</span><span> read(){
        </span><span>$data</span> = <span>$this</span>-><span>readData();
        </span><span>if</span>(<span>$data</span>->isRead && !<span>$data</span>-><span>isExpired())
            </span><span>return</span> <span>$data</span>-><span>data;
        </span><span>return</span> <span>null</span><span>;
    }

    </span><span>protected</span> <span>function</span><span> readData(){
        </span><span>$redis</span> = Rpt::<span>createRedis();
        </span><span>$rptData</span> = <span>new</span><span> RptData();
        </span><span>$data</span> = json_decode(<span>$redis</span>->get(<span>$this</span>-><span>key</span><span>));
        </span><span>if</span>(<span>false</span> == <span>$data</span><span>){
            </span><span>$rptData</span>->isRead = <span>false</span><span>;
            </span><span>$rptData</span>->expiredTime = <span>time</span><span>();
            </span><span>$rptData</span>->expired = 24*3600<span>;
        }</span><span>else</span><span>{
            </span><span>$rptData</span>->expired = <span>$data</span>-><span>expired;
            </span><span>$rptData</span>->isRead =  <span>$data</span>-><span>isRead;
            </span><span>$rptData</span>->expiredTime = <span>$data</span>-><span>expiredTime;
            </span><span>$rptData</span>->data = <span>$data</span>-><span>data;
        }
        </span><span>return</span> <span>$rptData</span><span>;
    }

    </span><span>/*</span><span>*
     * 同步读取数据
     * @return mixed
     </span><span>*/</span>
    <span>public</span> <span>function</span><span> readSync(){
        </span><span>while</span>(<span>true</span><span>){
            </span><span>$rptData</span> = <span>$this</span>-><span>readData();
            </span><span>if</span>(<span>$rptData</span>->isRead && !<span>$rptData</span>-><span>isExpired())
                </span><span>return</span> <span>$this</span>-><span>read();
            </span><span>sleep</span>(1<span>);
        }
    }
}

</span><span>/*</span><span>*
 * Class RptStorage  数据存储
 </span><span>*/</span>
<span>class</span><span> RptStorage {

    </span><span>/*</span><span>*
     * @var string key
     </span><span>*/</span>
    <span>public</span> <span>$key</span><span>;

    </span><span>/*</span><span>*
     * @param string $key
     </span><span>*/</span>
    <span>public</span> <span>function</span> __construct(<span>$key</span><span>){
      </span><span>$this</span>-><span>key</span> = <span>$key</span><span>;
    }

    </span><span>/*</span><span>*
     * 写入数据
     * @param $data
     * @param int $expired
     * @return bool
     </span><span>*/</span>
    <span>public</span> <span>function</span> save(<span>$data</span>,<span>$expired</span>=7200<span>){

        </span><span>$rptData</span> = <span>new</span><span> RptData();
        </span><span>$rptData</span>->data = <span>$data</span><span>;
        </span><span>$rptData</span>->expiredTime = <span>time</span><span>();
        </span><span>$rptData</span>->isRead = <span>true</span><span>;
        </span><span>$rptData</span>->expired = <span>$expired</span><span>;
        </span><span>$redis</span> = Rpt::<span>createRedis();

        </span><span>return</span> <span>$redis</span>->setex(<span>$this</span>-><span>key</span>, <span>$rptData</span>->expired,json_encode(<span>$rptData</span><span>));
    }
}

</span><span>/*</span><span>*
 * Class RptData redis存储数据实体
 </span><span>*/</span>
<span>class</span><span> RptData {
    </span><span>public</span> <span>$expired</span><span>;
    </span><span>public</span> <span>$expiredTime</span><span>;
    </span><span>public</span> <span>$data</span><span>;
    </span><span>public</span> <span>$isRead</span><span>;

    </span><span>public</span> <span>function</span><span> isExpired(){
        </span><span>if</span>(<span>time</span>()-<span>$this</span>->expiredTime > <span>$this</span>-><span>expired)
            </span><span>return</span> <span>true</span><span>;
        </span><span>return</span> <span>false</span><span>;
    }
}

</span><span>/*</span><span>*
 * Class RptGenerate key生成
 </span><span>*/</span>
<span>class</span><span> RptGenerate {
    </span><span>public</span> <span>static</span> <span>function</span> <span>key</span>(<span>$key</span>,<span>$nick</span><span>){
        </span><span>return</span> <span>$key</span>.".".<span>md5</span>(<span>$nick</span><span>);
    }
}</span>
Salin selepas log masuk

 

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan