Hi Tite, hi all,
As I've mentioned before i've made an ibatis cache controller for
memcached, for testing.
To do the same you have to modify the iBatis Mapper source code.
I suggest you to:
.download memcached v.1.1.13-pre-win32
.download .net client DLL, memcacheddotnet_clientlib-1.1.2
.download iBatis, Source Revision 426164
in IbatisDataMapper Project, under Configuration/Cache folder
create a new folder Memcached and a new MemcachedController.cs
using System;
using System.Collections;
using Memcached.ClientLibrary;
namespace IBatisNet.DataMapper.Configuration.Cache.Lru
{
public class MemcachedController : ICacheController
{
MemcachedClient mc;
double expireInMinutes = 0;
public object this[object key]
{
get
{
string sKey = key.ToString();
if (mc.KeyExists(sKey))
return mc.Get(sKey); // Return object
else
return null; // object not exists
}
set
{
string sKey = key.ToString();
if (value == null)
mc.Delete(sKey); // Remove object
else
if (expireInMinutes == 0)
mc.Set(sKey, value); // Store object no expire
else
mc.Set(sKey, value,
DateTime.Now.AddMinutes(expireInMinutes)); // Store object with expire
}
}
public object Remove(object key)
{
string sKey = key.ToString();
object o = null;
if (mc.KeyExists(sKey))
{
o = mc.Get(sKey);
mc.Delete(sKey);
}
return o;
}
public void Flush()
{
mc.FlushAll();
}
public void Close()
{
// ICacheController should have a method like this.
SockIOPool.GetInstance(mc.PoolName).Shutdown();
}
public void Configure(IDictionary properties)
{
string poolName = GetValue(properties["PoolName"], "default");
SockIOPool pool = SockIOPool.GetInstance(poolName);
pool.SetServers(GetValue(properties["ServerList"],
"127.0.0.1:11211").Split(','));
pool.InitConnections =
int.Parse(GetValue(properties["InitConnections"], "3"));
pool.MinConnections =
int.Parse(GetValue(properties["MinConnections"], "3"));
pool.MaxConnections =
int.Parse(GetValue(properties["MaxConnections"], "10"));
pool.SocketConnectTimeout =
int.Parse(GetValue(properties["SocketConnectTimeout"], "1000"));
pool.SocketTimeout =
int.Parse(GetValue(properties["SocketTimeout"], "3000"));
pool.MaintenanceSleep =
int.Parse(GetValue(properties["MaintenanceSleep"], "30"));
pool.Failover =
bool.Parse(GetValue(properties["Failover"], "true")); // if a server
is down go to the next one
pool.Nagle = bool.Parse(GetValue(properties["Nagle"],
"true")); // http://en.wikipedia.org/wiki/Nagle%27s_algorithm Nagle's
algorithm is a means of improving the efficiency of TCP/IP networks by
reducing the number of packets that need to be sent over the network
pool.Initialize();
mc = new MemcachedClient();
mc.EnableCompression = false;
mc.PoolName = poolName;
expireInMinutes =
double.Parse(GetValue(properties["minutes_standard_expire"], "0.0"));
}
private string GetValue(object value, string defaultValue)
{
if (value == null) return defaultValue;
else return value.ToString();
}
}
}
now you have to modify sqlmap.xsd, in order to include a new cache type
<xs:attribute name="implementation" use="required">
<xs:simpleType>
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="LRU"/>
<xs:enumeration value="MEMORY"/>
<xs:enumeration value="FIFO"/>
<xs:enumeration value="MEMCACHED"/> <!-- ** NEW ** -->
</xs:restriction>
</xs:simpleType>
</xs:attribute>
now change the cache factory ... in DomSqlMapBuilder.cs, find the
following region
#region Cache Alias
TypeAlias cacheAlias = new
TypeAlias(typeof(MemoryCacheControler));
cacheAlias.Name = "MEMORY";
_configScope.SqlMapper.TypeHandlerFactory.AddTypeAlias(cacheAlias.Name,
cacheAlias);
cacheAlias = new TypeAlias(typeof(LruCacheController));
cacheAlias.Name = "LRU";
_configScope.SqlMapper.TypeHandlerFactory.AddTypeAlias(cacheAlias.Name,
cacheAlias);
cacheAlias = new TypeAlias(typeof(FifoCacheController));
cacheAlias.Name = "FIFO";
_configScope.SqlMapper.TypeHandlerFactory.AddTypeAlias(cacheAlias.Name,
cacheAlias);
// Add the following lines ***** NEW LINES ***
cacheAlias = new TypeAlias(typeof(MemcachedController));
cacheAlias.Name = "MEMCACHED";
_configScope.SqlMapper.TypeHandlerFactory.AddTypeAlias(cacheAlias.Name,
cacheAlias);
now you can configure and use your new cache system ....
<cacheModels>
<cacheModel id="MyCache" implementation="MEMCACHED">
<flushInterval hours="24"/>
<flushOnExecute statement="test-insert"/>
<flushOnExecute statement="test-update"/>
<flushOnExecute statement="test-delete"/>
<!-- Specific properties -->
<property name ="poolName" value="default"/>
<property name ="minutes_standard_expire" value="1"/>
<!-- property name ="ServerList"
value="server1:port,server2:port,server3:port"/-->
</cacheModel>
run memcached.exe -vv
and run your ibatis software, you will be able to see on the command
shell, all the GETs and SETs of cached objects.
your business objects must have [Serializable()] attribute, and lazy
load have to be false.
you can "setup" any parameters of SockIOPool form the ibatismapper xml file.
I know there's a large improvements that can be made, this code is for
functionality testing only.
Actually i'm experiencing thereading problems, with multiple winform
clients, but I'm not sure if the source of the problem is my
macheController implementation.
Also, in my opinion, the method Close() have to be called
automatically by ibatis on system exit, in order to close connections
against the memcached servers.
Please let me know, if you have any suggestions, in order to improve
functionality and stability.
Does the iBatis core team plan to support and implement a distributed
cache system ?
Enjoy!
Giancarlo
On 10/25/06, Tite Etoundi <[EMAIL PROTECTED]> wrote:
Hi all,
about caching in iBATIS, I've downloaded memcacheddonet library and I've
find how someone can write an ICacheController as Ron's last post hereafter.
But, my question is how can I, after writing MemcachedCacheController as Ron
did, make iBATIS uses this controller instead of the default ones: LRU,
MEMORY and FIFO. I've seen SqlMap.xsd it recognizes only LRU, MEMORY and
FIFO. But, as I saw in that file, I could insert another cache
implementation like MC for MemcachedCacheController . The problem is that
somewhere it is asked to provide the implementation type as type alias, so
where can someone tell iBATIS to use MemcachedCacheController ?
Thanks,
Tite
Ron Grabowski <[EMAIL PROTECTED]> a écrit :
The ICacheController interface only has a few members to it. I didn't see
anything in the MemcachedClient about removing multiple keys.
I haven't tested the code below but I'm able to run the MemcachedBench
program against a win32 build of memcached:
http://jehiah.com/projects/memcached-win32/
I think the ICacheController should have a Shutdown member so 3rd party
caches can clean up when the application shutsdown.
Of course all the properties on the pool object should be settable by
the properties argument. The properties argument should also have a key
to prefix items put into the cache so IBatisNet plays nice with other
programs using the cache. It should be possible to also integrate the
FlushInterval construct into this implementation.
// untested - inspired by
Memcached.MemcachedBench.MemcachedBench
public class MemcachedCacheController : ICacheController
{
private SockIOPool pool;
private MemcachedClient memcachedClient;
public object this[object key]
{
get { return memcachedClient.Get(Convert.ToString(key)); }
set { memcachedClient.Set(Convert.ToString(key), value); }
}
public object Remove(object key)
{
return memcachedClient.Delete(Convert.ToString(key));
}
public void Flush()
{
// empty
}
public void Configure(IDictionary properties)
{
string servers = Convert.ToString(properties["server"]);
pool = SockIOPool.GetInstance();
pool.SetServers(servers.Split(','));
pool.InitConnections = 3;
pool.MinConnections = 3;
pool.MaxConnections = 5;
pool.SocketConnectTimeout = 1000;
pool.SocketTimeout = 3000;
pool.MaintenanceSleep = 30;
pool.Failover = true;
pool.Nagle = false;
pool.Initialize();
memcachedClient = new MemcachedClient();
memcachedClient.EnableCompression = false;
}
}
----- Original Message ----
From: Alexandre Grenier
To: [email protected]
Sent: Friday, October 6, 2006 2:51:41 PM
Subject: RE: distributed cahing/iBATIS.Net
I've done some fooling around with memcached... I still haven't looked into
how to write a custom cache provider for ibatis though. If no one's done it
by the time I get there, then I'll probably do it :)
Alex
-----Original Message-----
From: Ron Grabowski [mailto:[EMAIL PROTECTED]
Sent: Thursday, October 05, 2006 7:22 PM
To: [email protected]
Subject: Re: distributed cahing/iBATIS.Net
Someone needs to write an IBatisNet cache client for memcached:
http://www.danga.com/memcached/
There is a closed source solution called NCache but its probably $$$.
----- Original Message ----
From: Tite Etoundi
To: [email protected]
Sent: Tuesday, October 3, 2006 7:27:27 PM
Subject: distributed cahing/iBATIS.Net
Hi evry body,
I am new in using iBATIS as a data mapper in the project. But it works well.
It allow me to have a better architecture of my application so it isolates
correctly the data access. But my concern about it is distributed caching.
Does iBATIS allows this kind of stuff or is there an open source solution
that can be used with iBATIS like OSCache for iBATIS java?
Thank
Découvrez un nouveau moyen de poser toutes vos questions quel que soit le
sujet ! Yahoo! Questions/Réponses pour partager vos connaissances, vos
opinions et vos expériences. Cliquez ici.
________________________________
Découvrez une nouvelle façon d'obtenir des réponses à toutes vos questions
! Profitez des connaissances, des opinions et des expériences des
internautes sur Yahoo! Questions/Réponses.