Improve the cache invalidation code

Since the Hash are sorted, some optimization can
be made