生成跨实例保持不变的对象的唯一ID

I am generating an object from the database using the following:

$game = $this->game_model->get_by('slug', 'some-title');

I am also using a cache library in Codeigniter where I write to a cache file by:

$this->cache->write($game, $cache);

where $game is the object and $cache is an identifier for that object.

I would like to generate a unique ID for this object to use as the cache name. Something like spl_object_hash but that will stay the same across subsequent instances.

For example if I do:

$cache = spl_object_hash($game);

// Cache object
$this->cache->write($game, $cache);

It will generate 000000006c5ce27300000000564a8706.cache as the unique ID, but if I reload the page, I get a different ID, which defeats the purpose of the cache.

How can I get a consistent unique ID for an object?

You can use serialize() and then hash it.

$cache = hash('sha1', serialize($game));