Clear Magento cache based on key pattern - magento

Is there a way in magento to clear specific parts of the cache whihc begin with a specific cache key.
I would like to clear everything in my module that have a key that begin with:
mynamespace_mymodule_
so the following will be clear from cache:
mynamespace_mymodule_asdaqasd
mynamespace_mymodule_qeqweq
mynamespace_mymodule_poipoi
etc
etc

Magento's caching system allows you to assign both cache key (unique id) and cache tags while saving your cache. Look at Mage_Core_Model_Cache::save method - 3d argument is cache tags array.
By assigning cache tags you'll have ability to remove all cache which is tagged with your cache tag at once. Mage_Core_Model_Cache::clean methid will take array of cache tags as an argument.

Related

How can I store raw json string in redis cache using laravel without php serialization?

When I cache json string like so cache()->rememberForever('globals', fn() => json_encode(['foo' => 'bar']));.
The value stored in cache is actually "s:13:\"{\"foo\":\"bar\"}\";" rather than "{\"foo\":\"bar\"}"?
Is there a way I can store string without php serialisation?
You would need to use the true cache storage like Redis::put(...). The cache facade(s) have a pretty helpful way of getting complex data in and out of cache. For instance you can cache models or associative arrays thru that facade and not worry about how it gets stringified behind the scenes. However, if you don't want that kind of helper/handling to cache and restore your variables - then use the caching storage directly.
You should be able to store the string using the Cache::forever function. From Laravel docs Storing Items Forever:
The forever method may be used to store an item in the cache permanently. Since these items will not expire, they must be manually removed from the cache using the forget method:
Cache::forever('key', 'value');
Given that, I would change your code to something like the following:
cache()->forever('globals', json_encode(['foo' => 'bar']));

Using laravels {{old}} on dynamically created inputs

I have a form which allows a user to create an unlimited number of fields. If this forms fails validation I want the user to return to this page with the form populated with their previous input values - i.e. I want these fields to persist.
With a normal form I could do this with {{ old 'title' }}, however, these additional fields are being generated through JavaScript and so I cannot add this PHP snippet. What is the best way for me to retrieve these previous input values?
3 ways to do this, cache, sessions and cookies.
cache and sessions are server side which is much better for security, however it will take extra time and effort for setting up, but if the data is not sensible and can be passed within cookies, better to the cookies.
The best thing about cookies for your current situation is: you can set it up directly from your front end JS code.

nginx cache. How to define list of urls to cache only?

I have big list of urls to cache, for example:
http://example.com?a=1&b=1
http://example.com?a=2&b=1
etc...
There are no rule to describe these, only by direct listing.
I want my nginx to cache only urls I defined in this list and ignore caching for other GET parameters. Is there a way?

Caching options

As I see Smarty uses caching ‘by template’.
Can I somehow provide an URL to it, so it would cache pages by the URL given?
Can I get is_cached() to work with that given URL?
And compare last_mtime of the cached file with some of my data and then say «it’s time to update the cache»?
As default Smarty uses file based caching while storing php-like files in the $cache_dir.
You can implement a custom cache function and register it, but that depends on your desire how deep you want to dive into Smarty.
A way easier approach would be to just add a bit of the urls name to the template filename, so in your template directory for example might look like.
/your/templates/url1.index.tpl
/your/templates/url2.index.tpl
...and then use Smartys caching according to your needs.

Sitecore HTML cache and external data sources

How would you handle a situation when you want to cache some sublayout that relies on external data(search index, etc.)?
Here's the example:
After updating content you click
"Publish" in Sitecore.
HTML cache is automatically cleared after
publishing.
Sublayout is rendered and put into the cache on
first request, but the search index
is not yet updated.
Search index is updated.
Until the next cache cleanup, sublayout will display the cached (obsolete) date.
Are there any simple ways to fix this issue?
I've got only one idea so far - trigger cache cleanup when the index update is over, but it might be complicated for many reasons.
You can cache the sublayout and vary by params, where you define the custom params. Those params can be a unique string from Lucene, e.g the last time it was rebuilt.
E.g.
<sc:sublayout ID="slNews" Path="NewsList.ascx" Cacheable="true" VaryByParm="true" runat="server" />
Note: the Sitecore code has a typo and it's "VaryByParm" not "VaryByParam"
In C#:
string lastIndexRebuild = GetLastRebuildTimeOfIndex().ToString();
slNews.Parameters = "lastIndexRebuild=" + lastIndexRebuild;
If you can somehow define a method to determine when the index was last rebuilt, you can use that as a parameter to define custom cache instances based on when the index freshness or staleness. You can even tack on additional parameters, like a datasource, etc.
John West has recently posted related blog post -
Schedule Expiration for Output Cache Entries with the Sitecore ASP.NET CMS
You can set cache expiration on sublayouts, in most cases it seems to be the easiest solution when your control relies on external datasource.

Resources