duanfazhun0383 2011-01-19 10:08
浏览 43

从数组/类中计算唯一的id

I use a data class to feed templates my data, i want to calculate a unique id from the data in the data class so i can check if the template with that data is allready in cache and then serve that version.

so a function to get an unique id from an array of a class would help me out

something like this works but is rather costly md5(serialize($classdata))

im hopeing there is some function to get the unique id without serializing all data, or at least not to have to in php

Thanks in advance, best, paul

edit: i celebrated to soon, the unique id is only the same in the current instance a restart of the same script makes another id, wich then ofcourse is not in cache testscript used:

<?php
class foo {}
$f = new foo;
print spl_object_hash($f);

ill explain in some more depth

class template_data implements IteratorAggregate, ArrayAccess, Countable {

    private $_data;

    //some methods for the overloaded classes
    //

    //the getId function
    public function getId() {
        return hash('md5',serialize($this->_data));
    }

}

$t = new template('file');
$d = new template_data('some data');
$t->addData($d);
$t->display();

now if the data given to the template engine is in cache it uses that version preventing to having to re-parse the template for the dataset

this is a simplistic view of the template_data, it is actually lazy loading and uses memcached dataid's so the data isnt actually fetched till it is used in the template

  • 写回答

3条回答 默认 最新

  • duancan1900 2011-01-19 10:13
    关注

    You could try spl_object_hash()

    From the docs

    This function returns a unique identifier for the object. This id can be used as a hash key for storing objects or for identifying an object.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?