简体   繁体   中英

PHP and reading speed

I have a native app: the app requires data from a server and the server runs PHP (it's a fairly basic shared hosting). When data is requested, PHP must verify if a token coming in POST from the app is in a list of tokens. The tokens are NOT in a database (that is: I can't query the database asking for the tokens).

I can do one of the following (NoSQL dbs are not an option):

  1. I have a PHP file with an array of tokens: the PHP file called by the app includes this file;

  2. in a directory I have n txt files named with the token name: I test if a file exists with the name coming from the app;

  3. I have a txt file containing the tokens: a regex searches for the token;

  4. I have a JSON file: PHP loads the JSON, then I verify if there's the token;

What I would like to know is: what's the fastest way to read data? Are there any faster ways? Tokens can be no more than 5000.

Thanks in advance!

Let me add my opinion here, as @hanshenrik suggested, the PHP file with an array of tokens will be the quicker approach, and one more thing, in PHP world Laravel is an infamous framework, and they do make use of this approach for translation. In laravel they use separate PHP files which returns an associative array of translation key pairs,

The project that I am currently working with has many humongous files which hold the translation data. yet no performance issues. Hope this helps

the hardcoded tokens in a dedicated .php file approach will definitely be the fastest way (consider the OPcache for instance, its already pre-compiled after the first execution). personally, i'd just use an SQLite db and PDO for this though, unless and until i have an acual performance problem. an sqlite db is just a file, after all. (and its easier to query, delete from, and add to, and extend, than a .txt file, and it takes care of modification concurrency issues for you... though you could easily do the same manually with a little flock() )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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