Post deleted but still present between the related posts

Topics: ASP.NET 2.0, Business Logic Layer, Controls, Themes
May 13, 2015 at 7:48 AM
Hi
I deleted one post but when i open another post i see still old post deleted in Related Posts.. if i open this i obtain this error... How can i solve?
Thankss
Fabry

Ooops! I can't find the page you're looking for

This one's down to me! Please accept my apologies for this - I'll see to it that the developer responsible for this broken link is given 20 lashes (but only after he or she has fixed this problem).

You can also try to search for the page you were looking for:___
May 13, 2015 at 10:55 AM
In short, you will need to manually refresh the blog cache.

Posts with related posts are stored in a dictionary as <Guid, string> where each key is a post id(Guid) and each “key value” being that posts related posts(represented by a string of HTML markup).
When a post is deleted, the related posts dictionary is checked and if the post id exists as a key, then the entry is removed. However, the deleted post may still remain as a reference in the value(markup) of some other post entry in the dictionary in the form of a link to the now deleted post.

This probably needs looking at, in the meantime you could change the PostSaved event handler in RelatedPosts.cs.

So for example, from standard PostSaved handler in BE 2.8
 private static void PostSaved(object sender, SavedEventArgs e)
        {
            if (e.Action != SaveAction.Update)
            {
                return;
            }

            var post = (Post)sender;
            if (RelatedPostsCache.ContainsKey(post.Id))
            {
                RelatedPostsCache.Remove(post.Id);
            }
        }
To
 private static void PostSaved(object sender, SavedEventArgs e)
        {           
            RelatedPostsCache.Clear();
        }
It's a sledgehammer approach, but should do the trick for now.
May 14, 2015 at 7:40 AM
Thanks Andy
but i must to apply your code permanently or only for to clean the cache?
May 14, 2015 at 12:21 PM
Specifically, it’s the related posts cache that needs refreshing.

If you don’t change code in RelatedPosts.cs then any time you delete a post you would need to clean the cache manually(not good).
So yes, change code in RelatedPosts.cs

It might be better to explicitly check for post deletion.
private static void PostSaved(object sender, SavedEventArgs e)
        {
            // If a post is deleted then it will be removed from the dictionary cache by key entry
            // but the deleted post may still be referenced as a link in the key value(related posts markup) of some other post entry.
            if (e.Action == SaveAction.Delete)
            {
                RelatedPostsCache.Clear();
                return;
            }       
            if (e.Action != SaveAction.Update)
            {
                return;
            }
            var post = (Post)sender;
            if (RelatedPostsCache.ContainsKey(post.Id))
            {
                RelatedPostsCache.Remove(post.Id);
            }
        }
As I say, the caching probably needs to be revised.
For example, say you delete a post and then decide to restore it - you would still need to manually refresh cache(I think).
May 15, 2015 at 2:32 PM
Thanks Andy i tried your code and work perfectly.. Old post deleted aren't visible in related posts Now.

Great!