2

I have a list of objects they have guids for ids. I want to use the ids in this list of objects to create a guid that I can use ensure that the list has not changed. If an object was removed/added the guid would be different. The thing that comes to mind is using a hash. Maybe I should just use a hash, but I am wondering if it is possible that this could be faster than generating a hash?

EDIT: I am getting this list from a stored procedure. Then storing the list of objects into memory cache. Each user is going to validate their local value in a cookie against this generated value to ensure that the list is still the same.

11
  • 1
    A hash sounds good to me. Commented Jun 4, 2015 at 14:59
  • 1
    why don't you subclass or wrap a list with your own add/remove functions? Commented Jun 4, 2015 at 14:59
  • 1
    @AlexeiLevenkov does that make it more clear. Commented Jun 4, 2015 at 15:09
  • 1
    @DeadlyChambers I see now - I think you should be looking for some sort of "last changed"/ "data version" auto-increment value to come along with data from DB... Hash of any kind does not guarantee uniqueness so if correctness is requirement hash of any kind not going to work. Commented Jun 4, 2015 at 15:16
  • 1
    @AlexeiLevenkov I ended up going with a guid just coming along with the list from the cache, but you got me there. Commented Jun 4, 2015 at 17:08

2 Answers 2

2

You can write your own class that accesses the list or hashset or any other collection backing this:

class GuidList // You can implement IReadOnlyList { public bool Dirty { get; private set;} // Omit if implementing `IReadOnlyList` public Guid[] Guids { get { return guidCollection.ToArray() } public void Add(Guid guid) // Do similarly for remove { Dirty = true; // implement logic } } 

This is a form of information hiding.

Sign up to request clarification or add additional context in comments.

6 Comments

Ahh yeah I see that. I am storing this list of objects in memory cache so we are just going to invalidate the cache.
that should be pretty easy to do in this pattern.
The adding and removing of Guids will not happen in this fashion. I need a value that identifies the list. Again maybe I just need to go to hash.
@DeadlyChambers hashing not going to help much with cache invalidation - tracking changes (as shown in this answer) OR using immutable data structures are likely better approaches.
@DeadlyChambers you can do that - this is merely an example. since you control this class you can design it how you want it to function.
|
1

When you need to know if object is exactly the same as before you can't rely on hashing alone - hash of an object allows you to definitely tell that objects are different, but it does not guarantee that objects are the same.

Generally there are several approaches to solve that:

  • frequently small rate of potential collisions on hash values is acceptable and you can just use hash that long enough to satisfy your requirement. Usually crypto-hash functions like SHA256 provide low enough collision rate to just live with it. Such value easily will fit in HTTP cookie if needed.
  • if objects are small enough (i.e. several K) sending whole object to client may be an option (will give you chance to perform exact comparison on postbacks).
  • if you can clearly version objects in some way that is guaranteed to be unique (modified date, current sequential update with auto-increment) for particular object you can keep just that piece of information. Usually such versioning information is small and easy to send in cookie.

Note: depending on your requirement you may need to encrypt/sign cookie values (possibly with salt) to prevent client code from tampering with your cookie values.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.