Help for cache dependencies on a collection of pages

Georg Trixl asked on November 12, 2024 20:02

We have trouble with the caching of our contacts when we query a list of items. Once we update one of the page type items the cache doesn't invalidate. As far as I understand it, I need a special dependency for the items in that path, but I didn't had success in building that dependency. I hope some one is able to help. Big thanks in advance

public async Task<IEnumerable<ContactModel>> GetContactsAsync(string path, IEnumerable<string> authors)
    {
        var builder = new CacheDependencyKeysBuilder(_siteRepository, _cacheDependenciesStore);
        path = path.TrimEndForPath();
        builder.PagePath(path, PathTypeEnum.Children);

        var retriever = await _pageRetriever.RetrieveAsync<Contact>(
            query =>
            {
                query
                .TopN(10)
                    .PathForChildren(path)
                    .Columns(new string[] { 
                    nameof(Contact.Image),
                    nameof(Contact.Text),
                    nameof(Contact.Email),
                    nameof(Contact.Phone),
                    nameof(Contact.Gender),
                    })
                    .OrderBy(nameof(Contact.NodeLevel), nameof(Contact.NodeOrder));
                if (authors.Any())
                    query.WhereIn(nameof(Contact.DocumentName), authors.ToList());
            }, cacheSettings => cacheSettings
         .Dependencies((items, csbuilder) => builder.ApplyDependenciesTo(key => csbuilder.Custom(key)))
         .Key($"GetContactsAsync|{path}|{string.Join(",", authors ?? Array.Empty<string>())}")
         .Expiration(TimeSpan.FromMinutes(60))
       ); 

        return retriever.Select(x => _mapper.Map<ContactModel>(x));
    }

Recent Answers


Juraj Ondrus answered on November 13, 2024 06:35

I would recommend using also the cache debug to see under which keys the data is cached and then touch given keys.

0 votesVote for this answer Mark as a Correct answer

Georg Trixl answered on November 13, 2024 12:20

Okay I got there following generated key

Key:
getcontactsasync|/contact-folter||sos-dedev-de-gtrx|de-de

and following dependencies generated by that code above

documentid|40069
documentid|40068
documentid|40067
cms.documenttype|byname|sos.contact
node|sos-dedev-de-gtrx|/contact-folter
node|sos-dedev-de-gtrx|/contact-folter|childnodes

And did manage to update the code to get a dependency key not as node|sos-dedev-de-gtrx|/contact-folter|childnodes but as nodes|sos-dedev-de-gtrx|/contact-folter|all

documentid|40069
documentid|40068
documentid|40067
cms.documenttype|byname|sos.contact
node|sos-dedev-de-gtrx|/contact-folter
nodes|sos-dedev-de-gtrx|/contact-folter|all

But when change one of the items in the /contact-folter the cache does not update when having nodes|sos-dedev-de-gtrx|/contact-folter|all as dependency

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.