SessionHandlerInterface::write 和 SessionUpdateTimestampHandlerInterface::updateTimestamp 有什么区别? [英] What is the difference between SessionHandlerInterface::write and SessionUpdateTimestampHandlerInterface::updateTimestamp?

查看:31
本文介绍了SessionHandlerInterface::write 和 SessionUpdateTimestampHandlerInterface::updateTimestamp 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP 会话逻辑有两个不同的 SessionHandlerInterfaceSessionUpdateTimestampHandlerInterface 接口,而 SessionUpdateTimestampHandlerInterface 接口在文档中仍未完全描述.>

SessionHandlerInterface::writeSessionUpdateTimestampHandlerInterface::updateTimestamp 函数感觉非常相似.两者都具有相同的参数(会话 ID 和会话数据)并返回一个布尔值.

这两个函数有什么区别?

实现这两个功能的代码应该如何不同?

如果没有提供给 updateTimestamp 函数的时间戳,代码应该如何更新时间戳?

我创建了一个 关于缺少文档的 PHP 错误.

解决方案

这两种方法非常相似,但在不同的情况下调用.

1)

没有SessionUpdateTimestampHandlerInterface的SessionHandler:

登录的用户请求一个页面,并加载其包含所有数据的会话.在请求期间,数据集被修改,一旦会话关闭,所有数据将再次写入存储(调用SessionHandlerInterface::write).因此,您将一遍又一遍地写入相同的数据.

带有 SessionUpdateTimestampHandlerInterface 的 SessionHandler:

相同的场景,但现在如果数据集未被修改,它将调用 SessionUpdateTimestampHandlerInterface::updateTimestamp.优点是现在您知道不需要再次编写数据集,只需更新现有数据集的生命周期即可.这可能会带来巨大的性能提升.

2)

这取决于您存储会话/数据的位置.SessionHandlerInterface::write 应该真正写入数据,并且 SessionUpdateTimestampHandlerInterface::updateTimestamp 如果可能的话应该只更新现有项目的生命周期.Symfony 有一些很好的示例.

3)

时间戳应该与最大生命周期相同,以便像正常写入一样在每个请求上重置"它.

所提到的文档目前并不好.在此 RFC 中可以找到一些不错的信息.

The PHP session logic has two distinct SessionHandlerInterface and SessionUpdateTimestampHandlerInterface interfaces while the SessionUpdateTimestampHandlerInterface interface is still not fully described in the docs.

The SessionHandlerInterface::write and SessionUpdateTimestampHandlerInterface::updateTimestamp functions feels quite similar. Both have the same arguments (session id and session data) and return a bool.

What is the difference between these two functions?

How should differ the code implementing these two functions?

How is the code supposed to update a timestamp if there is no timestamp provided to the updateTimestamp function?

Edit: I have created a PHP bug regarding the missing docs.

解决方案

These two methods are quite similar but are called in different situations.

1)

SessionHandler without SessionUpdateTimestampHandlerInterface:

A logged in user requests a page and its session with all data gets loaded. During the request the dataset is not modified and once the session closes all data will be written into the storage again (calls SessionHandlerInterface::write). So you will write the same data over and over again.

SessionHandler with SessionUpdateTimestampHandlerInterface:

Same scenario but now if the dataset is not modified it will call SessionUpdateTimestampHandlerInterface::updateTimestamp. The advantage is now you know you don't need to write the dataset again and you just can update the lifetime of the existing dataset. This can be a huge performance improvement.

2)

Thats depending on where you are storing the sessions/data. SessionHandlerInterface::write should really write the data and SessionUpdateTimestampHandlerInterface::updateTimestamp if possible should just update the lifetime of an existing item. Symfony has some good examples.

3)

The timestamp should be the same as the max lifetime to "reset" it on every request as normal write would do.

The documation as mentioned is not good at the moment. Some good information can be found in this RFC.

这篇关于SessionHandlerInterface::write 和 SessionUpdateTimestampHandlerInterface::updateTimestamp 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆