Caching json setting files

29 views
Skip to first unread message

ML

unread,
Oct 10, 2015, 7:20:02 AM10/10/15
to Lucee
Hi,

I have some components that need to read setting files that are in json format upon init.  The files are fairly static, so I was thinking that I should cache them to minimize file reads.  What would be the recommended way to cache them? 

Thank you.

ML

Chris Blackwell

unread,
Oct 10, 2015, 9:46:04 AM10/10/15
to Lucee

Cache them in the application scope?  

How you go about that is upto you, it could simply be by accessing the application scope directly from your component although this probably isn't the "right" way to do it.

If you are using a framework with dependency injection (coldbox, fw1 etc) then you should wrap access to your configs using a caching loader class, then inject that into any components that require the config.

If you're using Coldspring (we still do!) then you can create a factory bean to load configs and define beans for the actual configs themselves, something like

<!-- parses json config and returns  -->
<bean id="ConfigReader" class="com.example.JsonConfigReader">
  <constructor-arg name="ConfigDirectory" value="/path/to/configs" />
</bean>

<!-- define a bean thats created by calling ConfigReader.getConfig('ServiceA.json'), this caches it inside coldspring -->
<bean id="ServiceAConfig" factory-bean="ConfigReader" factory-method="getConfig">
  <constructor-arg name="filename" value="ServiceA.json" />
</bean>

<!-- ServiceA singleton, gets passed the config into its contructor -->
<bean id="ServiceA" class="com.example.ServiceA">
  <constructor-arg name="config" ref="ServiceAConfig" />
</bean>


If the components that need to read the config are not singletons, then you should create a factory for them that injects the config when it creates a new instance, then replace any new cfc() or createobject() calls with calls to factory.new() etc..

HTH,
Chris



--
See Lucee at CFCamp Oct 22 & 23 2015 @ Munich Airport, Germany - Get your ticket NOW - http://www.cfcamp.org/
---
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+un...@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lucee/55a9c1b4-0e51-40f9-9823-753bd8950655%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages