Wordpress/PHP中的持久对象 [英] Persistent Objects in Wordpress/PHP

查看:46
本文介绍了Wordpress/PHP中的持久对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一组持久对象,这些持久对象从数据库中加载它们的状态,然后持久保存在内存中,以供Wordpress/PHP页面加载用作缓存的内存对象.我可以想象这些对象的接口包括:

I would like to create a set of persistent objects that load their state from the database and are then persisted in memory for Wordpress/PHP page loads to use as cached memory objects. I would imagine an interface for these objects to include:

  • initialise()-从数据库加载状态并执行在处理请求之前所需的任何其他初始化功能
  • getter_foo()-用于PHP代码的一系列getter方法,以调用内存缓存的响应
  • getter_bar()-用于PHP代码的一系列getter方法,以调用内存缓存的响应
  • update()-由时间或事件驱动的过程调用,这些过程要求对象返回数据库并刷新其状态

我怀疑有两个窍门:

  1. 为这些对象分配主要的PHP进程并保存其内存引用,以使它们在Web事务/请求中始终固定在内存中,而无需每次都针对数据库进行重新初始化
  2. 具有一种机制,该机制允许事务处理获取指向该对象的指针.

是否有解决方案的示例可以做到这一点?我已经编程多年了,但是对Wordpress和PHP来说都是新手,所以也许这很简单.没有把握.无论如何,我确实认识到诸如 redis memcached 之类的技术解决方案可能会达到相似的目标,但是方式不太优雅且没有上下文限制.就是说,如果没有简单的方法可以做到这一点,我很乐意使用80/20规则.:^)

Are there any examples of solutions that do this? I've been programming for years but am very new to both Wordpress and PHP so maybe this is quite straight forward. Not sure. In any event, I do recognise that technical solutions like redis and memcached might achieve similar goals but in a less elegant and non-contextual way. That said, if there's no easy way to do this I'm happy to use the 80/20 rule. :^)

推荐答案

在1个请求期间无法将数据存储在内存中,然后在另一个请求期间仅使用纯PHP不能从内存中读取数据.当然,PHP进程使用内存,但是一旦您的请求完成,那部分内存就会被垃圾回收.这意味着第二个请求无法再次访问该内存的先前部分.

It's not possible to store data in memory during 1 request, and then read it back from memory during another request using nothing but plain PHP. Sure the PHP process uses memory, but as soon as your request is finished, that part of the memory gets garbage collected. Which means that a second request cannot access that previous part of the memory again.

您要暗示的内容称为缓存.简而言之,缓存意味着您可以保存昂贵事务的输出以供以后重用,以节省该事务的成本.然后,用作后端存储该输出的内容取决于您自己或可用的内容.如果要将其保存到RAM,则需要像Memcached这样的东西.您也可以将其存储在常规文件中,但是由于要访问硬盘驱动器,因此速度较慢.

What you are hinting at, is called caching. Simply put, caching means that you save the output of an expensive transaction for later re-use, to save on the cost of that transaction. What you then use as a backend to store that output is up to you or what you have available. If you want to save it to the RAM, then you would need something like Memcached. You could also store it in regular file, but that is slower because of the hard drive being accessed.

这篇关于Wordpress/PHP中的持久对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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