
search + caching in CodeIgniter
Hi Habr and to everyone who uses the CodeIgniter PHP framework!
In the process of creating a certain CI application, I had a question about finding information in a database without using GET.
I did not find any ready-made solutions in the CI wiki, so I decided to create my own “bike”.
To begin, consider the examples of links that we can see when searching on different sites:
Example of links passing parameters via GET
Another example:
If we search by one parameter, then the link looks more or less normal
But if we need to search for something, for example, by 5 or more parameters?
And then our linkbecomes terrible and makes us look for another solution.
An example of parameter passing links via POST (so far only =))
Actually in such a link there are no parameters for the search,
only the identifier of the search result already stored in the cache.
If we pass parameters through the POST array, then our link will have
almost the same form with different incoming search parameters. It remains only to solve the
"problems and difficulties" of such a search method.
To solve this, we need CI + “any caching class” and a little time.
And so, how will our system work:
1. You need to create a unique key to save the search results in the cache.
(the link for viewing search results will look something like this:
sitename / search / query / 7ad549053bf2be45f309920b7b6a24bf / 10 )
2. Make a request to the database.
3. Save the result in the cache.
4. Display the search result to the user.
I would like to give a couple of code examples here,
but there is a lot of code, so I
’ll provide a link to download a ready-made controller for searching users + my own caching library and test database.
Download
In the process of creating a certain CI application, I had a question about finding information in a database without using GET.
I did not find any ready-made solutions in the CI wiki, so I decided to create my own “bike”.
To begin, consider the examples of links that we can see when searching on different sites:
Example of links passing parameters via GET
- sitename / search /? q = text
- sitename / index.php? do = search & p1 = value1 & p2 = value2 & uname = test
Another example:
- sitename / search / query / p1 : 0: p2: value: p3: text
If we search by one parameter, then the link looks more or less normal
But if we need to search for something, for example, by 5 or more parameters?
And then our link
An example of parameter passing links via POST (so far only =))
Actually in such a link there are no parameters for the search,
only the identifier of the search result already stored in the cache.
If we pass parameters through the POST array, then our link will have
almost the same form with different incoming search parameters. It remains only to solve the
"problems and difficulties" of such a search method.
To solve this, we need CI + “any caching class” and a little time.
And so, how will our system work:
1. You need to create a unique key to save the search results in the cache.
(the link for viewing search results will look something like this:
sitename / search / query / 7ad549053bf2be45f309920b7b6a24bf / 10 )
2. Make a request to the database.
3. Save the result in the cache.
4. Display the search result to the user.
I would like to give a couple of code examples here,
but there is a lot of code, so I
’ll provide a link to download a ready-made controller for searching users + my own caching library and test database.
Download