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

时间:2022-11-25 12:53:50

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.

我使用数据类来为我的数据提供模板,我想从数据类中的数据计算一个唯一的id,这样我就可以检查具有该数据的模板是否已经在缓存中,然后提供该版本。

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

所以从类数组中获取唯一id的函数可以帮助我

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

像这样的东西工作,但相当昂贵的md5(序列化($ 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

我希望有一些功能来获取唯一的ID而不序列化所有数据,或至少不必在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:

编辑:我很快就庆祝,唯一的id在当前实例中只是相同的,重新启动相同的脚本会生成另一个id,然后当然不在缓存testcript中使用:

<?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

这是一个简单的template_data视图,它实际上是延迟加载并使用memcached dataid,所以数据实际上并没有被提取,直到它在模板中使用

3 个解决方案

#1


5  

You could try spl_object_hash()

你可以尝试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.

此函数返回对象的唯一标识符。该id可以用作用于存储对象或用于识别对象的散列键。

#2


2  

PHP does not create unique IDs that persist between executions for objects, this means that you are going about producing the desired behavior correctly. So while there is no good answer for the asked question I can give some suggestions to reduce the cost of producing your IDs.

PHP不会创建在对象的执行之间持久存在的唯一ID,这意味着您将正确地生成所需的行为。因此,虽然对于提出的问题没有好的答案,但我可以提出一些建议,以降低生成ID的成本。

First you can use json_encode rather than serialize. Second, you can store the value, so that multiple calls to the function will not re-serialize the data every time.

首先,您可以使用json_encode而不是序列化。其次,您可以存储该值,以便多次调用该函数不会每次都重新序列化数据。

The json_encode function is not only faster than serialize, but it also produces a shorter string as output.

json_encode函数不仅比序列化更快,而且还产生一个更短的字符串作为输出。

http://cw-internetdienste.de/2015/05/04/serialize-vs-json_encode/

http://cw-internetdienste.de/2015/05/04/serialize-vs-json_encode/

class template_data implements IteratorAggregate, ArrayAccess, Countable {

    private $_data;
    private $_id;

    //
    //some methods for the overloaded classes
    //

    //the getId function
    public function getId() {
        if(empty($this->_id))
            $this->_id = hash('md5',json_encode($this->_data));

        return $this->_id;
    }
}

Lastly; the best solution will probably be to cache the output of the template using the route, or arguments as the basis for the unique cache keys rather than the individual data sets used.

最后;最好的解决方案可能是使用路由缓存模板的输出,或者参数作为唯一缓存键的基础而不是使用的单个数据集。

#3


1  

Why not look into and overriding the __toString() method on the object to get and hash the relevant data in the object.

为什么不查看并覆盖对象上的__toString()方法来获取和散列对象中的相关数据。

For example

例如

class Object
{
    // Some vars
    public $name = "Jake";
    public $age = 26;
    public $dob = "1/1/10"

    // the toString method
    public function __toString()
    {
         return md5($this->name . $this->age . $this->dob);
    }
}

// Create new object
$object = new Object();

// echo the object, this automatically calls your __toString method
echo $object

In this situation you don't use serialize, which is costly, instead just use __toString() to generate your own unique id based on variables stored with the object.

在这种情况下,您不使用序列化,这是昂贵的,而只是使用__toString()根据与对象一起存储的变量生成您自己的唯一ID。

#1


5  

You could try spl_object_hash()

你可以尝试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.

此函数返回对象的唯一标识符。该id可以用作用于存储对象或用于识别对象的散列键。

#2


2  

PHP does not create unique IDs that persist between executions for objects, this means that you are going about producing the desired behavior correctly. So while there is no good answer for the asked question I can give some suggestions to reduce the cost of producing your IDs.

PHP不会创建在对象的执行之间持久存在的唯一ID,这意味着您将正确地生成所需的行为。因此,虽然对于提出的问题没有好的答案,但我可以提出一些建议,以降低生成ID的成本。

First you can use json_encode rather than serialize. Second, you can store the value, so that multiple calls to the function will not re-serialize the data every time.

首先,您可以使用json_encode而不是序列化。其次,您可以存储该值,以便多次调用该函数不会每次都重新序列化数据。

The json_encode function is not only faster than serialize, but it also produces a shorter string as output.

json_encode函数不仅比序列化更快,而且还产生一个更短的字符串作为输出。

http://cw-internetdienste.de/2015/05/04/serialize-vs-json_encode/

http://cw-internetdienste.de/2015/05/04/serialize-vs-json_encode/

class template_data implements IteratorAggregate, ArrayAccess, Countable {

    private $_data;
    private $_id;

    //
    //some methods for the overloaded classes
    //

    //the getId function
    public function getId() {
        if(empty($this->_id))
            $this->_id = hash('md5',json_encode($this->_data));

        return $this->_id;
    }
}

Lastly; the best solution will probably be to cache the output of the template using the route, or arguments as the basis for the unique cache keys rather than the individual data sets used.

最后;最好的解决方案可能是使用路由缓存模板的输出,或者参数作为唯一缓存键的基础而不是使用的单个数据集。

#3


1  

Why not look into and overriding the __toString() method on the object to get and hash the relevant data in the object.

为什么不查看并覆盖对象上的__toString()方法来获取和散列对象中的相关数据。

For example

例如

class Object
{
    // Some vars
    public $name = "Jake";
    public $age = 26;
    public $dob = "1/1/10"

    // the toString method
    public function __toString()
    {
         return md5($this->name . $this->age . $this->dob);
    }
}

// Create new object
$object = new Object();

// echo the object, this automatically calls your __toString method
echo $object

In this situation you don't use serialize, which is costly, instead just use __toString() to generate your own unique id based on variables stored with the object.

在这种情况下,您不使用序列化,这是昂贵的,而只是使用__toString()根据与对象一起存储的变量生成您自己的唯一ID。