I'm relatively new to WordPress.

I have this 2.6 MB json file: https://raw.githubusercontent.com/codeforamerica/naics-api/master/data/naics-2012.json

It's a list of around 2000 entries where every entry is essentially a code paired with a description. The code is used to classify a given company.

Here is an example item from the list:

  "title":"Soybean Farming",
    "Soybean farming, field and seed production"
      "text":"Establishments engaged in growing...",
    "This industry comprises establishments primarily engaged in growing soybeans and/or producing soybean seeds."

I want to parse this file, stem each word in the description, and then search each item in response to a user query. Stemming is a somewhat expensive operation so ideally I would just do it once and store the results in a single global object which I could easily query across every request. I'm not sure how to set this up with WordPress.

Using a full-blown search engine seems like massive overkill. Even using an in-memory database seems like overkill. Really all I want is a global php array that I can create once when WordPress is initialized, keep in memory, and then refer to across all requests. Is it possible to set such a thing up? I really like the speed and simplicity of this approach and would rather avoid having to integrate with a database cache or something like that if at all possible.

I looked at this question: Global Objects and Public Methods

And the answer there suggests using a singleton pattern. But if I take that approach, it's not clear to me whether the class is instantiated once upon WordPress application launch or once for every request. I would like to avoid the latter.

I looked at this question: Keeping Objects in Memory

And it suggests using transients, but I'd like to avoid the performance hit of writing to a database. The answer further suggests connecting the transients API to memcached, but again setting up that infrastructure seems like overkill.

  • 1
    Has anyone suggested to you the WordPress object cache (wp_cache_set(), wp_cache_get(), etc.)? But you can use the singleton method with hooks like plugins_loaded which is supposed to be called only once per page load/request. – Sally CJ May 17 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.