简体   繁体   English

PHP 有没有办法像在 Java EE 中一样使用持久数据? (在 PHP 线程之间共享对象)没有会话也没有缓存/数据库

[英]Is there a way in PHP to use persistent data as in Java EE? (sharing objects between PHP threads) without session nor cache/DB

Is there a way in PHP to use "out of session" variables, which would not be loaded/unloaded at every connexion, like in a Java server ?在 PHP 中有没有办法使用“会话外”变量,它不会像在 Java 服务器中那样在每次连接时加载/卸载?

Please excuse me for the lack of accuracy, I don't figure out how to write it in a proper way.请原谅我缺乏准确性,我不知道如何以正确的方式编写它。

The main idea would be to have something like this :主要想法是有这样的东西:

<?php
    ...
    // $variablesAlreadyLoaded is kind of "static" and shared between all PHP threads
    // No need to initialize/load/instantiate it.
    $myVar = $variablesAlreadyLoaded['aConstantValueForEveryone'];
    ...
?>

I already did things like this using shmop and other weird things, but if there is a "clean" way to do this in "pure PHP" without using caching systems (I think about APC, Redis...), nor database.我已经使用shmop和其他奇怪的东西做了这样的事情,但是如果有一种“干净”的方式在“纯 PHP”中做到这一点,而不使用缓存系统(我想到 APC,Redis ...),也不使用数据库。

EDIT 1 :编辑 1:

Since people (thanks to them having spent time for me) are answering me the same way with sessions, I add a constraint I missed to write : no sessions please.由于人们(感谢他们为我花时间)在会话中以同样的方式回答我,我添加了一个我错过的约束:请不要会话。

EDIT 2 :编辑 2:

It seems the only PHP native methods to do such a thing are shared memory ( shmop ) and named pipes.似乎唯一可以做这种事情的 PHP 本地方法是共享内存 ( shmop ) 和命名管道。 I would use a managed manner to access shared objects, with no mind of memory management (shared memory block size) nor system problems (pipes).我会使用托管方式来访问共享对象,而不用考虑内存管理(共享内存块大小)或系统问题(管道)。

Then, I browsed the net for a PHP module/library which provides functions/methods to do that : I found nothing.然后,我在网上浏览了一个 PHP 模块/库,它提供了执行此操作的函数/方法:我什么也没找到。

EDIT 3 :编辑 3:

After a few researches on the way pointed out by @KFO, it appears that the putenv / setenv are not made to deal with objects (and I would avoid serialization).后在路上的几个研究中指出@KFO,似乎在putenv / setenv都没有作出处理对象(我会避免序列化)。 Thus, it resolves the problem for short "things" such as strings or numbers but not for more large/comples objects.因此,它解决了诸如字符串或数字之类的短“事物”的问题,但不能解决更大/复杂的对象的问题。

Using the "env way" AND another method to deal with bigger objects would be uncoherent and add complexity to the code and maintenability.使用“env 方式”和另一种方法来处理更大的对象将是不连贯的,并且会增加代码和可维护性的复杂性。

EDIT 4 :编辑 4:

Found this : DBus ( GREE Lab DBus ), but I'm not having tools to test it at work.发现这个:DBus( GREE Lab DBus ),但我没有工具在工作中测试它。 Has somebody tested it yet ?有人测试过了吗?

I'm open to every suggestion.我对每一个建议持开放态度。

Thanks谢谢

EDIT 5 ("ANSWER"):编辑 5(“答案”):

Since DBus is not exactly what I'm looking for (needs to install a third-party module, with no "serious" application evidence), I'm now using Memcache which has already proven its reliability (following @PeterM comment, see below).由于 DBus 并不是我正在寻找的(需要安装第三方模块,没有“严重”的应用程序证据),我现在正在使用已经证明其可靠性的 Memcache(遵循@PeterM 评论,见下文)。

// First page
session_id('same_session_id_for_all');
session_start();
$_SESSION['aConstantValueForEveryone'] = 'My Content';

// Second page
session_id('same_session_id_for_all');
session_start();
echo $_SESSION['aConstantValueForEveryone'];

This works out of the box in PHP.这在 PHP 中开箱即用。 Using the same session id (instead of an random user-uniqe string) to initialize the session for all visitors leads to a session which is the same for all users.使用相同的会话 id(而不是随机的用户唯一字符串)为所有访问者初始化会话会导致所有用户都相同的会话。


Is it really necessary to use session to achieve the goal or wouldn't it better to use constants ?是否真的有必要使用 session 来实现目标,或者使用常量不是更好吗?

There is no pure PHP way of sharing information across different threads in PHP!在 PHP 中没有跨不同线程共享信息的纯 PHP 方式! Except for an "external" file/database/servervariable/sessionfile solution.除了“外部”文件/数据库/服务器变量/会话文件解决方案。


Since some commentators pointed out, that there is serialize/unserialize functionality for Session data which might break data on the transport, there is a solution: In PHP the serialize and unserialize functionality serialize_handler can be configured as needed.由于一些评论者指出,会话数据的序列化/反序列化功能可能会破坏传输中的数据,因此有一个解决方案:在 PHP 中,可以根据需要配置序列化和反序列化功能serialize_handler See https://www.php.net/manual/session.configuration.php#ini.session.serialize-handler It might be also interesting to have a look at the magic class methods __sleep() and __wakeup() they define how a object behaves on a serialize or unserialize request.参见https://www.php.net/manual/session.configuration.php#ini.session.serialize-handler看看魔术类方法__sleep()__wakeup()它们定义如何对象在序列化或反序列化请求上表现。 https://www.php.net/manual/language.oop5.magic.php#object.sleep ... Since PHP 5.1 there is also a predefined Serializable interface available: https://www.php.net/manual/class.serializable.php https://www.php.net/manual/language.oop5.magic.php#object.sleep ... 从 PHP 5.1 开始,还有一个预定义的可Serializable接口可用: https : //www.php.net/manual/ class.serializable.php

You can declare a Variable in your .htaccess.您可以在 .htaccess 中声明一个变量。 For Example SetEnv APPLICATION_ENVIRONMENT production and access it in your application with the function getenv('APPLICATION_ENVIRONMENT')例如SetEnv APPLICATION_ENVIRONMENT production并在您的应用程序中使用函数getenv('APPLICATION_ENVIRONMENT')访问它

Another solution is to wrap your variable in a "persistent data" class that will automatically restore its data content every time the php script is run.另一种解决方案是将变量包装在“持久数据”类中,该类将在每次运行 php 脚本时自动恢复其数据内容。 Your class needs to to the following:你的班级需要做到以下几点:

  • store content of variable into file in __destructor将变量的内容存储到__destructor文件中
  • load content of variable from file in __constructor__constructor文件加载变量的内容

I prefer storing the file in JSON format so the content can be easily examined for debugging, but that is optional.我更喜欢以 JSON 格式存储文件,以便可以轻松检查内容以进行调试,但这是可选的。

Be aware that some webservers will change the current working directory in the destructor, so you need to work with an absolute path.请注意,某些网络服务器会更改析构函数中的当前工作目录,因此您需要使用绝对路径。

I think you can use $_SESSION['aConstantValueForEveryone'] that you can read it on every page on same domain.我认为您可以使用$_SESSION['aConstantValueForEveryone'] ,您可以在同一域的每个页面上阅读它。

Consider to refer to it's manual .考虑参考它的手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM