Can I data cache an IQueryable<> collection? - linq-to-sql

can this be cached?

No, you would need to enumerate it (e.g. using ToList()) and cache the results of the enumeration. Caching the IQueryable object itself is effectively just caching the query: in that scenario it will still be re-executed every time you try to use the results.

Related

How to invalidate redis cache with mysql?

I want to use redis as cache for mysql, and the main idea is:
Query
read from redis
if not exist, read from mysql, and add to redis cache
Add
write to mysql directly
Update&Delete
write to mysql
invalidate the cache of redis
My question is: how to invalidate the cache?
I know I can delete it, or set a expire time, is it a usual way, or are there any standard methods to invalidate the cache?
Thanks!
You would need a trigger to tell redis that mysql data has been updated.
This can be a part of your code, whenever you save data to mysql, you invalidate the redis cache as well.
You can use streams like http://debezium.io/ to capture changes in the database, and take neccessary actions like invalidating cache.

Which Hibernate Queries Hits Second Layer Cache?

I just added Memcached as my second layer cache for Hibernate. Performance actually took a significant hit after installing the cache. All queries are slower. I realized that the reason is probably due to most of my queries aren't based on id, so second layer cache is not being used.
My question is shouldn't non id-based queries just go straight to the database without ever hitting the cache? Aka, the decision making of whether the query is "cache appropriate" be determined prior to hitting the cache? If so, shouldn't performance be faster?
When I was checking Hibernate code, it looked like Hibernate cannot reuse cache when using HQL queries (it didn't have compiler from HQL to their caching mechanism).
I can recommend you rather use fjorm instead of Hibernate. Disclaimer: I'm a founder of fjorm.

Ruby on Rails: Why first active record query takes longer?

If i execute active record query after some time gap, it takes longer.
Say Item.all takes .11 sec on first query and .003 later on. what could be possible reason for this behaviour?
edited:
active record query cache 's scope is action of controller. In my case, active record query in subsequent http request is also faster.
Possible explanations:
ActiveRecord Caching
Connection Pooling (it doesn't have to restart the connection)
Load on the web server or db server.
ActiveRecord caches the results from queries. The first query is actually hitting the database - ActiveRecord then waits for the operation to complete and parses the results into its objects. The next time an identical query is made, it has the results cached so that they are returned to you immediately, instead of going all the way back to the database.
Check the API for the QueryCache: it seems like you can clear the query cache (connection.clear_query_cache) if you want to wipe out cached queries.
This SO question also suggests self.class.uncached do ... end to bypass the cache but I am not sure if this still applies in Rails 3.
It's definitely ActiveRecord's caching you're looking at. See doc.
All of the methods are built on a simple caching principle that will
keep the result of the last query around unless specifically
instructed not to. The cache is even shared across methods to make it
even cheaper to use the macro-added methods without worrying too much
about performance at the first go.

How to efficiently cache web service requests-response pairs in JSON format

I find a interesting problem when working with web service in JSON format.
Assume there's web service. accept several parameters. each parameter has different value set. You can get the response by passing different request parameters.
The request is in JSON format. Because there're so many different combination of request parameters. For performance optimization, I want to cache the request and response pair. and store it into local database. If there's big hash table, I may want to store the request as key, the response as value.
I am thinking the MongoDB maybe a solution. But I am not sure. Is it possible to store request-response as key-value pair in these kind of database? So I can cache the result and response to user immediately.
Thank you.
You won't get any benefit from that level of caching unless your code and database have spectacularly bad performance (in which case you have bigger problems than setting up a cache).
You can use JSON as a key with any key/value store, though it probably makes sense to use a hash as the cache key rather than using the JSON string directly, and a non-persistent in-memory cache with memcached or redis will work a lot better than a complete document database like MongoDB.
Where you will run into big problems with this approach is managing cache expiry - to get real time updates, you need to know exactly which cached objects are affected by a change to a given object. That's easy if the request is a simple get by ID, but next to impossible in the scenario you describe.
The other way to manage cache is expiry is to delete objects from the cache after a given time. This assumes that it is acceptable to show stale data after an update. Caches usually have support for expiry built in. Databases generally don't.

How to cache results

I heard that a lot of big project administrators caches results. I wonder how do I do it? Thanks
I'm using PHP.
Pseudo-logic:
Test if query result exists in cache
If so,
retrieve from cache
return result
else
execute query against database
transfer results to an array, list or object(s) [dependent on language]
store results in cache (serialize if necessary)
return result
Often memcache or APC (if you're using PHP) can be used to store the cache, but files can be used at a pinch. Code will vary depending on what medium you're using for cacheing
In a tipical mysql - php environment memcached can be a good choice.
memcached FAQ