You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "Calbazana, Al" <AC...@epotec.com> on 2002/09/14 06:11:09 UTC

mod_perl and Cache::SharedMemCache

General question re the Cache::SharedMemeoryCache module and mod_perl...

If I have several objects loaded into an in-memory cache, would there be any
contention if multiple client's request the same object from cache
simultaneously?  I guess I am wondering if there would be a need to write
"pooling" code to manage objects selected from cache vs. grabbing a copy out
of cache and simply using it.

I'd like to share the same set of objects across multiple apache child
processes.  Basically, the each object represents a command/action object.
Although there is not a huge initialization cost, I'd really hate to have to
create an instance of them each and every time a request comes in,
especially since they could be used throughout the application.  I wouldn't
be storing any state in the cached objects, just the initialized object
itself.  Anyone getting a copy from cache would do with it as it wished and
then move on about their business.

Thanks,

Al



**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


Re: mod_perl and Cache::SharedMemCache

Posted by Perrin Harkins <pe...@elem.com>.
Calbazana, Al wrote:
> General question re the Cache::SharedMemeoryCache module and mod_perl...

General answer: use Cache::FileCache instead, it is much faster.

> If I have several objects loaded into an in-memory cache, would there be any
> contention if multiple client's request the same object from cache
> simultaneously?

That's not how it works.  The cache is a shared memory segment which is 
not able to hold Perl variables.  Cache::SharedMemoryCache (and 
IPC::Shareable and IPC::ShareLite and everything based on them) works by 
serializing the perl variables into a string of bytes with Storable, 
writing that string of bytes into the shared memory segment, and then 
reading it back out at some future time, possibly in a different 
process, and restoring it with Storable.

Cache::Cache does atomic updates, but it has no concept of locking and 
the object that you receive when you fetch from the cache has no ongoing 
relationship to the object in the cache.  In other words, nothing 
prevents every process from simultaneously grabbing the same object from 
the cache, making changes to it willy nilly and then saving it back 
again, overwriting each other and losing each other's updates.  In 
practice this is not a problem for most applications of Cache::Cache, 
where "last save wins" is the desired effect.  If you need mutually 
exclusive access to stored objects, you should look at something else, 
like MLDBM::Sync or BerkeleyDB.

> Although there is not a huge initialization cost, I'd really hate to have to
> create an instance of them each and every time a request comes in,
> especially since they could be used throughout the application.  I wouldn't
> be storing any state in the cached objects, just the initialized object
> itself.

Storing these objects in Cache::Cache or any persistent storage module 
will be much slower than keeping them in memory.  Object initialization 
costs are usually trivial compared to I/O operations.  People use 
Cache::Cache for things like storing the results of slow queries or 
calculations.

I think you may be prematurely optimizing here.  I would suggest you 
build your application without caching, and then profile it with 
Devel::DProf and see what the slow parts are.  It's very unlikely that 
initializing objects with static data will be one of them.

- Perrin