跨PHP请求本机持久化数据 [英] Natively persisting data across PHP requests

查看:50
本文介绍了跨PHP请求本机持久化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在PHP请求中缓存一些数据,而不是在每次调用时从数据库中提取数据.数据普遍适用,与用户/会话无关.实现此目的最简单的方法是什么?我不能保证对驱动器的写访问权限或需要APC.有没有办法用香草PHP做到这一点?谢谢!

I want to cache some data across PHP requests rather than pull it from the database during each call. The data applies universally regardless of user/session. What is the lightest method of achieving this? I cannot guarantee write access to the drive or require APC. Is there a way to acheive this with vanilla PHP? Thanks!

推荐答案

当您问这个问题时,我想到了3种方法",但在总结之前,我先说一下PHP并非旨在做些什么你要.没有用于查询(或累积)数据的通用"缓存系统.但是,您可以做一些事情来加快页面加载和数据请求的速度:

There are 3 'ways' that come to my mind when you ask this, but before I sum them up let me say that PHP is not designed to do what you want. There is no 'universal' caching system for queried (or accumulated) data. However, you can do several things to speed up your page loads and data requests:

  1. 会话(要求:对用户的Cookie支持并为每个新用户重新查询)
  2. 缓存文件(要求:对驱动器的写访问权限)
  3. 缓存查询(要求:能够在MySQL中缓存查询)
  1. Session (Requirements: Cookie support for a user and requeries for every new user)
  2. Cache File (Requirements: Write Access to the drive)
  3. Caching the Query (Requirements: Able to cache the query in MySQL)

第一个会话很明显,但是仅当数据取决于用户时才应使用它(如果不尝试避免使用该数据).对于缓存文件,您需要写入权限,但这是站点范围内数据的最佳方法.只需使用适合您需要的序列化数组(纯文件)或XML数据即可.最后一个是加快MySQL请求的好方法(如果每次它们都是相同的,并且您对系统没有任何写访问权).您可以使用

The first sessions is rather obvious, but you should only use it if the data is user dependent (if not try to avoid it). For caching file you will need write access, but this is the best method for site wide data. Just use either serialized arrays (plain file) or XML data, whatever suits your needs. The last one is a great way to speed up MySQL requests (if they are the same every time and you don't have any write access to the system). You cah cache the query in the database, by using cached select.

如您所见,所有这些方法都有其优点和缺点,但是您可以随意使用它们.但是,有一个补充说明是从文件或会话中加载缓存的数据需要花费时间,因此您应该检查缓存是否实际上会加快请求的速度!

As you can see all these methods have their own advantages and disadvantages, but you can use them all however you like. One side-note however is that it takes time to load cached data from file or session, so you should check whether caching actually speeds up requests!

这篇关于跨PHP请求本机持久化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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