21. December 2013 19:38
Sometimes you need to delete all dictionary items from the Umbraco dictionary.
Deleting 100’s of dictionary items can be a PITA. Imagine you have to right click for every dictionary item and select delete.
First you probably want to know which tables the dictionary is using. There are 2: one for the items, one for the translations. They call cmsDictionary and cmsLangaugeText.
If you ever want to get information from the dictionary, just join these tables and you have what you need.
select d.pk, lt.pk, d.[key], lt.languageId, l.languageISOCode, lt.value
from cmsDictionary d inner join cmsLanguageText lt on d.id = lt.UniqueId
left join umbracoLanguage l on lt.languageId = l.id
where d.pk = 6
The fastest way to remove all umbraco dictionary items is through SQL. To remove ALL dictionary items, just run this SQL script:
delete from cmsLanguageText
delete from cmsDictionary
Don’t forget to touch the web.config because dictionary items are heavily cached!
13. August 2013 14:52
I was looking at some code from Zack Owens “Multi Tenancy Sample”. He has some clever code, a ThreadSafeDictionary with a GetOrAdd method. I love the idea! (code below for my own reference)
The problem with a dictionary, is that – apparently - .Net puts somewhere a system wide lock to ensure thread safety on a dictionary anyway. That’s the reason that, if you are working with huge dictionaries with lots of READS, you might bump into some performance issues. In such cases, you could (and should) use the ConcurrentDictionary from the System.Collections.Concurrent namespace. And if you are looking at this class, visit the BlockingCollection from the same namespace also, still trying to wrap my head around it, but it is ultra important. You just feel it when reading through the MSDN article.
/// DON’T USE THIS CODE – You probably want a System.Collections.Concurrent.ConcurrentDictionary
/// Dictionary that has a "GetOrAdd" method that is thread-safe
/// <typeparam name="TKey">Dictionary key</typeparam>
/// <typeparam name="TValue">Dictionary value</typeparam>
public class ThreadSafeDictionary<TKey, TValue> : Dictionary<TKey, TValue>
/// Lock for adding values
private ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim();
/// Gets a value or adds the value in a thread-safe way
/// <param name="key">Key in the dictionary</param>
/// <param name="defaultValue">Delegate that will get the value</param>
/// <returns>Value from the dictionary with given <paramref name="key"/></returns>
public TValue GetOrAdd(TKey key, Func<TValue> defaultValue)
// enter read lock
// test if value is in the dictionary
// exit read lock
// enter write lock
// exit write lock