douyan1921 2013-04-30 17:36
浏览 59
已采纳

生成跨实例保持不变的对象的唯一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?

  • 写回答

1条回答 默认 最新

  • douyi1779 2013-04-30 17:54
    关注

    You can use serialize() and then hash it.

    $cache = hash('sha1', serialize($game));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗