turbine-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "jill han" <j...@bynum.com>
Subject RE: caching object
Date Thu, 14 Jun 2007 21:53:27 GMT
I still have a problem in getting the cached object. Here are the codes.

private Physician loadPhysician(RunData data, Object id) throws
Exception
    {
        GlobalCacheService gs = null;
	  CachedObject obj = null;
        try
        {
            /*
             * Look for the item in the cache.
             * If it doesn't exist or the item is stale,
             * the cache will throw an exception.
             */
            gs = (GlobalCacheService) TurbineServices.getInstance()
                .getService(GlobalCacheService.SERVICE_NAME);
            obj = gs.getObject("cached_physician");
        }
        catch(ObjectExpiredException gone)
        {
            Criteria crit = new Criteria();
            crit.add(PhysicianPeer.OBJECT_ID, id);

            List v = PhysicianPeer.doSelect(crit);
            Physician object = null;

            if (v.size() > 0)
            {
                object = (Physician) v.get(0);
            }
            else
            {
                object = new Physician(this.getClient(data));
            }
            /*
             * Add the physician to the cache.
             */
            gs.addObject("cached_physician",
                new CachedObject(object,5000));
		return object;
        }
	
	  return ??????

    }

How can the cached physician object be returned?
Since I can't just cast CachedObject into Physician like 
return (Physician)obj

Appreciate your help.
Jill
-----Original Message-----
From: Thomas Vandahl [mailto:tv@apache.org] 
Sent: Sunday, June 10, 2007 12:36 PM
To: Turbine Users List
Subject: Re: caching object

jill han wrote:
> In the usage section, 
> data.setMessage( data.getScreen() + " Got " +
>         obj.getContents().toString() + " from global cache!" );
> 
> what kind of object is data? Is it RunData object?

Yes. However this is just an example that stands for a real usage of the
cached object. Whatever you put into the cache will be returned by
obj.getContents(). The example shows the pattern:
- try to get the object from the cache
- if it's not there, an ObjectExpiredException is thrown
- in the catch()-branch create the object, wrap it into a CachedObject
and put it into the cache
- then return the value of the cached object.

Hope it helps.
Bye, Thomas.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@turbine.apache.org
For additional commands, e-mail: user-help@turbine.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@turbine.apache.org
For additional commands, e-mail: user-help@turbine.apache.org


Mime
View raw message