2 questions:
Can I insert(map)?
and can I cache results from a selet to memory, say in softmap that
decays after a minute?
.V
That's an interesting idea for insert/update/delete operations, but
you'd need to either specify a table name or a class mapped to a
table. Something like insert("customer", mapCustomer). I'm afraid this
could be problematic for auto-generated keys, but as long as you don't
have keys in your map that are mapped to auto-generated keys in the
table, things should go smooth. I'll add this feature in the next
release. Please let me know if you have some specific needs for this
feature.
As for caching, I prefer to leave that concern out of the Persist
scope. Managing a cache of objects automatically is a very complex
business, and ultimatelly would require a custom query language (as
Hibernate uses). iBATIS has a simpler "all-or-nothing" approach, which
is fine for content that is rarely modified but useless otherwise.
That said, it's actually simple to add caching to your DAO objects in
case you need it (which here means: when you're *optimizing* your
persistence layer). You could use something like ehcache with a helper
class, the code to do so would be something like that:
public static Cache getCache(String cacheName) {
CacheManager manager = CacheManager.getInstance();
Cache cache = manager.getCache(cacheName);
if (cache==null) {
manager.addCache(cacheName);
cache = manager.getCache(cacheName);
}
return cache;
}
public static Map<String, Object> readMap(Persist persist, String
sql, Object...parameters) {
Cache cache = getCache("[map] [sql=" + sql + "]");
String parametersString = (parameters==null) ? "" :
Arrays.toString(parameters);
Element element = cache.get(parameters);
if (element == null) {
element = new Element(parametersString, persist.readMap(sql,
parameters));
cache.put(element);
}
return (Map<String,Object>) element.getObjectValue();
}
public static void purgeMap(Persist persist, String sql,
Object...parameters) {
Cache cache = getCache("[map] [sql=" + sql + "]");
String parametersString = (parameters==null) ? "" :
Arrays.toString(parameters);
cache.remove(parametersString);
}
You'll find a proof of concept project that contains code for caching
POJOs and Maps in the files are of this group (http://
groups.google.com/group/persist/web/persist-cache.zip). You can use
this as a start point for a helper suited to your needs. I think it's
more flexible (and probably more clear) to have calls to a caching
helper in your DAO objects than expecting "magic" behavior from your
ORM tool. Using this approach you can use any caching scheme you want,
either doing caching by PK (as EJB/CMP and Hibernate use), by query
(as iBATIS use) or using a mixed approach (since you know what's being
changed in a given statement, you know what to purge).
Please let me know if this works for you.
Julio
On Sep 13, 5:44 pm, "vcekven...@gmail.com" <vcekven...@gmail.com>
wrote:
.V