Home Page Categories model cache

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 years ago
Hi Andrei,

a customer of ours reported an issue, that I tested after that and seemed really odd. When you setup a Home Page Categories panel, then change the dimensions of the images from the Media Settings (Category thumbnail image size) and click Save, nothing actually happens. No matter how many times you refresh the page, the setting doesn't do anything. That is really inconsistent with the way the other Media Settings work and even the rest of the Administration.

What fixed it was clicking Clear cache/Restart application and then if you refresh the page you will see the new dimensions of the category images. It turns out that the HomepageCategories action caches the whole list of category models and after the media settings are changed, the model is taken from the cache.

I think it is more intuitive to have this settings' effect visible immediately, without the need of something as major as Clear cache/Restart application.

Regards,
Aleks
8 years ago
Hi Aleks,

Thanks a lot for reporting! I've just created a work item
7 years ago
Problem solved. Implementation details can be found in the commit #c0d09c5
7 years ago
Sergei-k wrote:
Problem solved. Implementation details can be found in the commit #c0d09c5


Thank you, Sergei! It looks great :)

Regards,
Aleks
7 years ago
Hi
I have same issue like this
I have created one ActionResult to get all call categories like this

  [NopHttpsRequirement(SslRequirement.No)]
        public ActionResult Collections()
        {

            var pictureSize = _mediaSettings.CategoryThumbPictureSize;
            //using cache here
            string categoriesCacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_COLLECTIONS_KEY,
                string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()),
                pictureSize,
                _storeContext.CurrentStore.Id,
                _workContext.WorkingLanguage.Id,
                _webHelper.IsCurrentConnectionSecured());

            var model = _cacheManager.Get(categoriesCacheKey, () =>
                _categoryService.GetAllCategories()
                .Select(x =>
                {
                    var catModel = x.ToModel();

                    //prepare picture model
                    var categoryPictureCacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_PICTURE_MODEL_KEY, x.Id, pictureSize, true, _workContext.WorkingLanguage.Id, _webHelper.IsCurrentConnectionSecured(), _storeContext.CurrentStore.Id);
                    catModel.PictureModel = _cacheManager.Get(categoryPictureCacheKey, () =>
                    {
                        var picture = _pictureService.GetPictureById(x.PictureId);
                        var pictureModel = new PictureModel
                        {
                            FullSizeImageUrl = _pictureService.GetPictureUrl(picture),
                            ImageUrl = _pictureService.GetPictureUrl(picture, pictureSize),
                            Title = string.Format(_localizationService.GetResource("Media.Category.ImageLinkTitleFormat"), catModel.Name),
                            AlternateText = string.Format(_localizationService.GetResource("Media.Category.ImageAlternateTextFormat"), catModel.Name)
                        };
                        return pictureModel;
                    });

                    return catModel;
                })
                .ToList()
            );

            if (!model.Any())
                return Content("");

            return View(model);
        }

I also created the key for cache


        public const string CATEGORY_COLLECTIONS_KEY = "Nop.pres.category.collections-{0}-{1}-{2}-3{}-{4}";
        public const string CATEGORY_COLLECTIONS_PATTERN_KEY = "Nop.pres.category.collections";


So if i change picture of category in admin then i go to front-end it does not change.It only change when i clear cache in admin
Not sure where is reason why?
Thank you
7 years ago
The problem is that your keys for caching don't have information about the images, but about their size only.
7 years ago
Can you help me how can fix in my code?
Thanks
7 years ago
bmsoft wrote:
Can you help me how can fix in my code?
Thanks


For example

[NopHttpsRequirement(SslRequirement.No)]
public ActionResult Collections()
{

    var pictureSize = _mediaSettings.CategoryThumbPictureSize;
    //using cache here
  
    var model = _categoryService.GetAllCategories()
        .Select(x =>
        {
            var catModel = x.ToModel();

            //prepare picture model
            var categoryPictureCacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_PICTURE_MODEL_KEY, x.Id, pictureSize, x.PictureId, _workContext.WorkingLanguage.Id, _webHelper.IsCurrentConnectionSecured(), _storeContext.CurrentStore.Id);
            catModel.PictureModel = _cacheManager.Get(categoryPictureCacheKey, () =>
            {
                var picture = _pictureService.GetPictureById(x.PictureId);
                var pictureModel = new PictureModel
                {
                    FullSizeImageUrl = _pictureService.GetPictureUrl(picture),
                    ImageUrl = _pictureService.GetPictureUrl(picture, pictureSize),
                    Title = string.Format(_localizationService.GetResource("Media.Category.ImageLinkTitleFormat"), catModel.Name),
                    AlternateText = string.Format(_localizationService.GetResource("Media.Category.ImageAlternateTextFormat"), catModel.Name)
                };
                return pictureModel;
            });

            return catModel;
        })
        .ToList();

    if (!model.Any())
        return Content("");

    return View(model);
}

public const string CATEGORY_COLLECTIONS_KEY = "Nop.pres.category.collections-{0}-{1}-{2}-{3}-{4}-{5}";
public const string CATEGORY_COLLECTIONS_PATTERN_KEY = "Nop.pres.category.collections";
7 years ago
The problem solved.
Thank you so much
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.