Key Question on Custom ICache Implementation

61 views
Skip to first unread message

kellyb

unread,
Mar 18, 2011, 1:19:52 PM3/18/11
to nhusers
We have our own caching framework that works well and we like it. We'd
like to wire it up to NHibernate rather than take a dependency one of
the other caching implementations for NH.

I've written an implementation of NHibernate.Cache.ICache that bridges
our framework with NH. I based my implementation almost entirely on
this:

https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHibernate.Caches/Velocity/NHibernate.Caches.Velocity/VelocityClient.cs

One thing that jumped out at me right off the bat is the cache keys in
the ICache interface are of type object. Our framework key's the cache
using strings. I noticed the Velocity implementation above just does
a .ToString() on the key instance that's passed to the Get(object key)
and Put(object key) methods. That made me a little nervous.

In my testing it appears the "key" object is of type string and it's
the entity name.

My questions:

* Is blindly calling .ToString() on the key instance a safe
approach (I'm checking for null)?
* If this implementation is indeed safe, does that mean it's
always a string?
* If this is always a string, then why is this of type object?

The NH guys are smarter than me, so the fact it's of type object makes
me assume there's something I'm missing here. I really don't want to
introduce some obscure, random session management bug with unsafe key
usage in my cache implementation.

Any guidance would be appreciated - cheers

kellyb

unread,
Mar 22, 2011, 6:54:18 AM3/22/11
to nhusers
Is there a better place to ask this question?

On Mar 18, 1:19 pm, kellyb <kelly.brownsber...@gmail.com> wrote:
> We have our own caching framework that works well and we like it. We'd
> like to wire it up to NHibernate rather than take a dependency one of
> the other caching implementations for NH.
>
> I've written an implementation of NHibernate.Cache.ICache that bridges
> our framework with NH. I based my implementation almost entirely on
> this:
>
> https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHi...

Fabio Maulo

unread,
Mar 22, 2011, 12:05:08 PM3/22/11
to nhu...@googlegroups.com
Key answer
Which is the problem to solve ?

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.




--
Fabio Maulo

kellyb

unread,
Mar 23, 2011, 4:28:19 AM3/23/11
to nhusers
Was there something unclear about my initial post?

I'm asking why the key is of type System.Object and what can I expect
to be passed in that value? My caching framework using key of type
System.String, and the NHContrib project's NHibernate.Caches
implementations seem to blindly call .ToString() on the passed in
key. Is that the prescribed method for handling the key in this
scenario?

On Mar 22, 12:05 pm, Fabio Maulo <fabioma...@gmail.com> wrote:
> Key answer
> Which is the problem to solve ?
>

kellyb

unread,
Mar 29, 2011, 11:40:59 PM3/29/11
to nhusers
In case someone else is interested in some answers on this topic...

I've been running our custom cache implementation now for more than a
week with a simple key.ToString() approach similar to the Velocity
implementation and it seems to be working well.

After reviewing the NH source code and using ReSharper to find all
usages of the Get() and Put() methods, it seems all objects being
passed have ToString() overrides that appear to be designed safely.
I've concluded that key.ToString() is safe.

Hope this is helpful to someone else.

On Mar 23, 4:28 am, kellyb <kelly.brownsber...@gmail.com> wrote:
> Was there something unclear about my initial post?
>
> I'm asking why the key is of type System.Object and what can I expect
> to be passed in that value?  My caching framework using key of type
> System.String, and the NHContrib project's NHibernate.Caches
> implementations seem to blindly call .ToString() on the passed in
> key.  Is that the prescribed method for handling the key in this
> scenario?
>
> On Mar 22, 12:05 pm, Fabio Maulo <fabioma...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Key answer
> > Which is the problem to solve ?
>
> > On Tue, Mar 22, 2011 at 7:54 AM, kellyb <kelly.brownsber...@gmail.com>wrote:
>
> > > Is there a better place to ask this question?
>
> > > On Mar 18, 1:19 pm, kellyb <kelly.brownsber...@gmail.com> wrote:
> > > > We have our own caching framework that works well and we like it. We'd
> > > > like to wire it up to NHibernate rather than take a dependency one of
> > > > the other caching implementations for NH.
>
> > > > I've written an implementation of NHibernate.Cache.ICache that bridges
> > > > our framework with NH. I based my implementation almost entirely on
> > > > this:
>
> > > >https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHi...
>
> > > > One thing that jumped out at me right off the bat is thecachekeys in
Reply all
Reply to author
Forward
0 new messages