{"id":18564896,"url":"https://github.com/bdcrops/module-cachetype","last_synced_at":"2025-05-15T18:34:54.395Z","repository":{"id":62492464,"uuid":"209534256","full_name":"bdcrops/module-cachetype","owner":"bdcrops","description":"Creating a new cache type in Magento is as easy as doing the following","archived":false,"fork":false,"pushed_at":"2019-09-20T01:01:38.000Z","size":127,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-26T12:09:50.028Z","etag":null,"topics":["magento2","magento2-module","new-cache-type"],"latest_commit_sha":null,"homepage":"https://www.bdcrops.com","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bdcrops.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-19T11:16:24.000Z","updated_at":"2019-09-20T18:24:38.000Z","dependencies_parsed_at":"2022-11-02T11:16:39.124Z","dependency_job_id":null,"html_url":"https://github.com/bdcrops/module-cachetype","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdcrops%2Fmodule-cachetype","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdcrops%2Fmodule-cachetype/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdcrops%2Fmodule-cachetype/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdcrops%2Fmodule-cachetype/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bdcrops","download_url":"https://codeload.github.com/bdcrops/module-cachetype/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239293948,"owners_count":19615043,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["magento2","magento2-module","new-cache-type"],"created_at":"2024-11-06T22:16:42.104Z","updated_at":"2025-02-17T13:14:39.307Z","avatar_url":"https://github.com/bdcrops.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BDC_CacheType\n\nThis module is used as creating a new cache type Magento 2 extensions.\nA cache type enables you to specify what is cached and enables merchants to clear that cache type using the Cache Management page in the Magento Admin.\n\nOne of the key things is to use Magento cache in a way that we can cache our rendered content on the frontend and flush only the specific blocks if needed. A custom cache type therefore enables us to specify what is cached and enables our clients to clear that specific cache type using the Cache Management option in the Administration.\n\n\n\n## Creating a new cache type in Magento is as easy as doing the following\n\n- Create app/code/BDC/CacheType/registration.php \u0026 insert below Code:\n```\n\u003c?php\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n    'BDC_CacheType',\n    __DIR__\n);\n\n```\n- Create app/code/BDC/CacheType/etc/module.xml\n```\n\u003c?xml version=\"1.0\"?\u003e\n\u003cconfig xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module/etc/module.xsd\"\u003e\n    \u003cmodule name=\"BDC_CacheType\" setup_version=\"1.0.0\"/\u003e\n\u003c/config\u003e\n\n```\n- Create app/code/BDC/CacheType/etc/cache.xml\n\n```\n\u003c?xml version=\"1.0\"?\u003e\n\n\u003cconfig xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Cache/etc/cache.xsd\"\u003e\n    \u003ctype name=\"bdcrops_cache\" translate=\"label,description\" instance=\"BDC\\CacheType\\Model\\Cache\\BDCropsCache\"\u003e\n        \u003clabel\u003eBDCrops Cache\u003c/label\u003e\n        \u003cdescription\u003eBDCrops custom cache type\u003c/description\u003e\n    \u003c/type\u003e\n\u003c/config\u003e\n\n```\n\n- Create app/code/BDC/CacheType/Model/Cache/BDCropsCache.php\n```\n\u003c?php\n\nnamespace BDC\\CacheType\\Model\\Cache;\n\nclass BDCropsCache extends \\Magento\\Framework\\Cache\\Frontend\\Decorator\\TagScope\n{\n    const TYPE_IDENTIFIER = 'bdcrops_cache';\n    const CACHE_TAG = 'BDCrops_CACHE';\n\n    public function __construct(\\Magento\\Framework\\App\\Cache\\Type\\FrontendPool $cacheFrontendPool) {\n        parent::__construct($cacheFrontendPool-\u003eget(self::TYPE_IDENTIFIER), self::CACHE_TAG);\n    }\n}\n\n```\n- Active Module, Clean \u0026 Test CLI \u0026 Admin\n```\nphp bin/magento setup:upgrade\nphp bin/magento cache:flush\nphp bin/magento cache:status\nphp bin/magento cache:enable CACHE_TYPE\nphp bin/magento cache:enable bdcrops_cache\n\n\n```\n\n\nCLI:\n![](docs/CacheStatusCli.png)\nAdmin:\n![](docs/CacheStatusAdmin.png)\n\n- To  use New  Cache Type:\nTo use this new cache type, you will need a helper file. This helper generates hash from cache ID and loads/saves content that depends on the cache ID\n\nCreate app/code/BDC/CacheType/Helper/Cache.php\n```\n\u003c?php\nnamespace BDC\\CacheType\\Helper;\n\nuse Magento\\Framework\\App\\Helper;\n\nclass Cache extends Helper\\AbstractHelper {\n    const CACHE_TAG = 'bdcrops_cache';\n    const CACHE_ID = 'megamenu';\n    const CACHE_LIFETIME = 86400;\n\n    protected $cache;\n    protected $cacheState;\n    protected $storeManager;\n    private $storeId;\n\n    /**\n     * Cache constructor.\n     * @param Helper\\Context $context\n     * @param \\Magento\\Framework\\App\\Cache $cache\n     * @param \\Magento\\Framework\\App\\Cache\\State $cacheState\n     * @param \\Magento\\Store\\Model\\StoreManagerInterface $storeManager\n     */\n    public function __construct(\n        Helper\\Context $context,\n        \\Magento\\Framework\\App\\Cache $cache,\n        \\Magento\\Framework\\App\\Cache\\State $cacheState,\n        \\Magento\\Store\\Model\\StoreManagerInterface $storeManager ) {\n        $this-\u003ecache = $cache;\n        $this-\u003ecacheState = $cacheState;\n        $this-\u003estoreManager = $storeManager;\n        $this-\u003estoreId = $storeManager-\u003egetStore()-\u003egetId();\n        parent::__construct($context);\n    }\n\n    /**\n     * @param $method\n     * @param array $vars\n     * @return string\n     */\n    public function getId($method, $vars = array()) {\n        return base64_encode($this-\u003estoreId . self::CACHE_ID . $method . implode('', $vars));\n    }\n\n    /**\n     * @param $cacheId\n     * @return bool|string\n     */\n    public function load($cacheId){\n        if ($this-\u003ecacheState-\u003eisEnabled(self::CACHE_ID)) {\n            return $this-\u003ecache-\u003eload($cacheId);\n        }\n\n        return FALSE;\n    }\n\n    /**\n     * @param $data\n     * @param $cacheId\n     * @param int $cacheLifetime\n     * @return bool\n     */\n    public function save($data, $cacheId, $cacheLifetime = self::CACHE_LIFETIME){\n        if ($this-\u003ecacheState-\u003eisEnabled(self::CACHE_ID)) {\n            $this-\u003ecache-\u003esave($data, $cacheId, array(self::CACHE_TAG), $cacheLifetime);\n            return TRUE;\n        }\n        return FALSE;\n    }\n}\n\n```\n##  FAQ Cache\n\n### Magento 2 - How To Save Custom Data To Cache?\nTo maintain the performance of the Magento 2 platform when working with data that is updated rarely or have a large volume, we can use the caching mechanism.\n\n- Create a model or use an existing one, and add the caching interface loading to the constructor.\n```\npublic function __construct(\n   Magento\\Framework\\DataObjectFactory $dataObjectFactory,\n   Magento\\Framework\\App\\CacheInterface $cache,\n   Json $serializer = null  ) {\n   $this-\u003edataObjectFactory = $dataObjectFactory;\n   $this-\u003ecache = $cache;\n   $this-\u003eserializer = $serializer ?: ObjectManager::getInstance()-\u003eget(Json::class);\n}\n```\n- To save the data in the cache, let's call the save method\n```\n$this-\u003ecache-\u003esave($data, $identifier, $tags, $lifeTime);\n```\n\nSince the caching mechanism can only store a string, the $data parameter is a string, if you need to save the array, you need to use the serializer.\n\nFirst, we serialize the data $data = $this-\u003eserializer-\u003eunserialize($data) and then transfer it to the cache for saving.\n\n$identifier - unique identifier of the stored information block.\n\n$tags = array of tags - may be empty.\n\n$lifeTime - the time (in seconds) through which the block will be considered not valid, by default null - it always appears in the cache and valid.\n\n- To get data from the cache\n```\n$this-\u003ecache-\u003eload($identifier)\n```\n\nIf there is no data or the lifetime has expired, the method returns false.\nDo not forget to convert the data after receiving it from the cache, if you have it serialized before recording\n\n### test?\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbdcrops%2Fmodule-cachetype","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbdcrops%2Fmodule-cachetype","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbdcrops%2Fmodule-cachetype/lists"}