Annyce Davis

Davis Technology Consulting

  • Home
  • About Me
  • Blog
  • Courses
  • Newsletter

Improved caching with Kotlin flows and the reduce function

March 17, 2022 by Annyce Davis

I’ve been on a caching crusade. I’m working to reduce the load on our database by focusing on various caching solutions. In an ideal world, data that is “static” will be heavily cached; minimal database interaction required. However, that’s not the world I’m living in. Each request to our GraphQL API leads directly to a database hit. Ugh!

Apollo GraphQL offers a few caching solutions to help with this endeavor. We decided to go with the responseCachePlugin. This plugin stores cacheable data in a datastore of your choice, for us that was Redis. The key thing to keep in mind is that the data must be cacheable. Which leads us to “the problem.”

Continue Reading

Storing JSON in External EhCache Server

July 11, 2011 by Annyce Davis

If you would like to store JSON data in an external EhCache Server you would need to use either REST or SOAP calls.  The below example uses REST calls and the HTTPBuilder library.  It assumes that you have your EhCache Server running at the following location:
http://localhost:8080/ehcache/rest.

The name of the cache that I’m using in this example is just sampleCache.

def getCachedItem(key) {
def uri = "http://localhost:8080/ehcache/rest/sampleCache/"
def restClient = new RESTClient(uri)

def res
try {
res = restClient.get(path:key)
}
catch (Exception e) {
log.error("Error retrieving item from cache for this key: $key")
}

return res?.data
}

def putCachedItem(key, html) {
def url = "http://localhost:8080/ehcache/rest/sampleCache/"

try {
def json = html as grails.converters.JSON

def http = new HTTPBuilder(url)
http.request(Method.PUT) { req ->
uri.path = key
send('application/json', grails.converters.JSON.parse(json?.toString()))

response.'201' = { resp ->
log.info("success in adding item to cache: ${resp.status}")
}

response.success = { resp, object ->
log.info("success in adding item to cache: ${resp.status}")
}

response.failure = { resp ->
log.error("error adding item to cache: ${resp.statusLine}")
}
}
}
catch (Exception e) {
log.error("Error setting the cache for this key: $key")
}
}

Monitor Hibernate Cache Statistics in Grails Application

March 18, 2010 by Annyce Davis

After going through the effort of adding cache:true all over your application to take advantage of the Hibernate caching you will want to monitor the results. In order to do so you only need to modify two files, DataSource.groovy and a desired Controller.

//In your DataSource.groovy
hibernate {
generate_statistics=true
}

//In your Controller
def sessionFactory

def showCacheStatistics = {
def statistics = sessionFactory.statistics
log.info(statistics)
render statistics
}

Here is the output of calling the showCacheStatistics method on your controller:
Statistics[start time=1269058393252,sessions opened=27,sessions closed=25,transactions=18,successful transactions=3,optimistic lock failures=0,flushes=21,connections obtained=15,statements prepared=219,statements closed=219,second level cache puts=45,second level cache hits=1,second level cache misses=0,entities loaded=40,entities updated=5,entities inserted=48,entities deleted=0,entities fetched=26,collections loaded=20,collections updated=0,collections removed=0,collections recreated=12,collections fetched=18,queries executed to database=93,query cache puts=6,query cache hits=8,query cache misses=6,max query time=89]

Currently I’m showing just a direct rendering of the statistics object, but you could specify which parameters are important to you and only show those, or perhaps send the data to a gsp file and have it formatted in some way.

Follow Me

  • Bluesky

Categories

  • Android (60)
  • Career (5)
  • Communication (4)
  • Flutter (1)
  • Git (4)
  • Gradle (4)
  • Grails (23)
  • iOS (1)
  • Java (8)
  • JavaScript (6)
  • Kotlin (17)
  • Life (5)
  • Public Speaking (26)
  • Revenue (2)
  • RxJava (1)
  • Software Development (13)
  • Twitter (3)
  • Uncategorized (11)
  • Video Course (5)

Follow Me

  • Bluesky

Copyright © 2025 · All Rights Reserved · Log in

 

Loading Comments...