PDA

View Full Version : Problem getting cache to work with web application



dbpete
04-01-2008, 02:25 PM
Hello All,
I'm new with Spring.Net and have managed to implement Spring.Net successfully in our web application. I have run into an issue that I can't seem to figure out and hopefully someone here can point out what I did wrong. I cannot get the Spring.Net caching to work. I receive no errors. I'm using the latest release of Spring.NET and here are the details specific to my setup:

Web.config settings:

<configSections>
<sectionGroup name="spring">
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHand ler, Spring.Core" />
<section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web" />
</sectionGroup>
</configSections>

<spring>
<parsers>
<parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
</parsers>
<context>
<resource uri="/config/Spring.xml" />
</context>
</spring>

Spring.xml:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:db="http://www.springframework.net/database">

<!-- Property placeholder configurer for database settings -->
<object type="Spring.Objects.Factory.Config.PropertyPlaceholderC onfigurer, Spring.Core">
<property name="ConfigSections" value="databaseSettings"/>
</object>

<!-- Production Data Access Objects -->

<db:provider id="dbProvider"
provider="SqlServer-2.0"
connectionString="Data Source=${db.server};Initial Catalog=${db.database};User ID=${db.user}; Password=${db.password}"/>

<object id="adoTemplate" type="Spring.Data.Core.AdoTemplate, Spring.Data">
<property name="DbProvider" ref="dbProvider"/>
</object>

<object id="myDao" type="myproject.core.dao.impl.MyDaoImpl, my.core">
<property name="AdoTemplate" ref="adoTemplate"/>
</object>

<object id="myServiceTarget" type="myproject.core.service.impl.MyServiceImpl, my.core">
<property name="myDao" ref="myDao" />
</object>

<object id="myService" type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop">
<property name="Target" ref="myServiceTarget" />
<property name="ProxyInterfaces">
<value>myproject.service.IMyService</value>
</property>
</object>


<!-- Caching Methods -->
<object id="CacheAspect" type="Spring.Aspects.Cache.CacheAspect, Spring.Aop"/>
<object id="AspNetCache" type="Spring.Caching.AspNetCache, Spring.Web">
<property name="SlidingExpiration" value="true"/>
<property name="Priority" value="Low"/>
<property name="TimeToLive" value="02:02:00"/>
</object>

<object type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxy Creator, Spring.Aop">
<property name="ObjectNames">
<list>
<value>*DaoImpl</value>
</list>
</property>
<property name="InterceptorNames">
<list>
<value>CacheAspect</value>
</list>
</property>
</object>
</objects>


MyDaoImpl.cs (dao layer)

namespace myproject.core.dao.impl
{
public class MyDaoImpl : AdoDaoSupport, IMyDao
{
private static readonly ILog logger = LogManager.GetLogger(typeof(MyDaoImpl));


[CacheResult("AspNetCache", "", TimeToLive = "5:1:0")]
public String GetStuff()
{
logger.Debug("INSIDE STUFF!!");
return "stuff";
}

}
}


MyServiceImpl.cs (service layer)

namespace myproject.core.service.impl
{
public class MyServiceImpl : IMyService
{
private IMyDao myDao;

public MyServiceImpl()
{
}

public MyServiceImpl(IMyDao myDao)
{
this.myDao = myDao;
}

public String GetStuff()
{
return myDao.GetStuff();
}

}
}

The aspx page that calls the MyService:

private void Page_Load(object sender, System.EventArgs e)
{
logger.Debug("Loading page");

IApplicationContext ctx = ContextRegistry.GetContext();
IMyService myService = ctx.GetObject("myService") as IMyService;

String stuff = myService.GetStuff();
}

Every page request causes the logging message in the DAO method regardless if I have caching applied or not. I'm assuming I do not have this configured correctly.

Thanks in advance for any help!

Daniel Hegener
04-03-2008, 09:39 AM
You should set the key to some meaningful value like this:
[CacheResult("AspNetCache", "'stuff'", TimeToLive = "5:1:0")]
And do not forget to add the single quotes around the key or you'll end up with a couple of nice exceptions (like I did...).

dbpete
04-03-2008, 12:45 PM
Thanks for the reply. I tried several different values in the key along with adding single quotes around the value. No matter what I try I still see the text "INSIDE STUFF!!" in my log file every time the aspx page is hit so I know it's still not being cached. Am I doing something else wrong here? Am I instantiating the context (IApplicationContext) incorrectly in the aspx file?

Bruno Baia
04-06-2008, 10:28 PM
Hi,

"*DaoImpl" does not match any object in your container.
ObjectNameAutoProxyCreator.ObjectNames defines a list of object names in the container that should be proxied, not the type names.

You should try "*Dao" instead, it matchs "myDao".


HTH,
Bruno

dbpete
04-08-2008, 05:40 PM
Thanks Bruno, that was the problem. I thought I had tried that value in the ObjectNames but I guess not! Thanks for the help.