How to Automatically Clear Cache in Shopware App System
You may encounter situations where you need to clear the cache in the Shopware app automatically. Such as when an update is made or cache issues are affecting your store’s performance. By default, Shopware requires a manual process to clear the cache. However, this can be automated through the use of app permissions and API calls.
Steps to automatically clear cache in the Shopware app system
To implement automatic cache clearing in your Shopware app, follow these steps:
Step 1. Add Permission to manifest.xml
In the manifest.xml
file of your app, you need to add the appropriate permission that allows the app to clear the cache. This is done by requesting the system:cache:info
permission.
Here is an example of how to add this permission to the manifest.xml
file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | `xml <?xml version="1.0" encoding="UTF-8"?> <manifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/shopware/trunk/src/Core/Framework/App/Manifest/Schema/manifest-2.0.xsd"> <meta> <!-- Your meta information here --> </meta> <permissions> <read>product</read> <create>product</create> <update>product</update> <delete>order</delete> <!-- Since version 6.4.12.0 your app can request additional non-CRUD privileges--> <permission>system:cache:info</permission> </permissions> </manifest> |
In the above XML code:
- We add the
<permission>system:cache:info</permission>
inside the<permissions>
section to allow the app to perform cache-related operations.
This will enable the app to use the clearAllCache
API function to perform automatic cache clearing.
Step 2. Create a Function to Call Admin API to Clear Cache
In your server-side code, you need to implement a function that triggers the cache clearing operation via Shopware’s Admin API. You can use the DELETE
method to clear the cache.
Here’s an example of a function that can be added to your server code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | ```php public function clearAllCache(): void { $request = $this->factory->createRequest('DELETE', sprintf( '%s/api/_action/cache', $this->shopUrl ))->withHeader('Content-Type', 'application/json'); $response = $this->httpClient->sendRequest($request); if ($response->getStatusCode() !== 204) { throw new Exception('Failed to clear cache: ' . $response->getBody()); } } |
In the above code:
- The
clearAllCache()
function sends aDELETE
request to the/api/_action/cache
endpoint of the Shopware Admin API to clear the cache.
It checks the response status code to ensure that the cache was successfully cleared. If the response code is not204
(No Content), an exception is thrown with an error message.
Step 3. Automate Cache Clearing
Now that you’ve set up the permission and created the API call, you can integrate this function wherever you need cache clearing in your application. You could trigger it upon specific events like updates, deployments, or any other relevant system processes.
Conclusion
By adding the correct permission to your manifest.xml
file and creating a function to call the Admin API, you can automatically clear the cache in the Shopware app system. This ensures that your Shopware app maintains smooth performance and prevents cache-related issues from affecting your store. If you require any technical support, feel free to reach out to our Shopware Agency support team for further assistance.
Recent help desk articles
Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.