Database cache implementation and generic sort of the cache
merged with vasilis