Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed to load caches from *CacheConfig.groovy classes (using an art…
…efact handler) in addition to Config.groovy
- Loading branch information
Burt Beckwith
committed
Apr 30, 2012
1 parent
0d6d250
commit d04b711
Showing
12 changed files
with
359 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
cacheConfig = { | ||
cache { | ||
name 'grailsBlocksCache' | ||
} | ||
cache { | ||
name 'grailsTemplatesCache' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
cacheConfig = { | ||
cache { | ||
name 'basic' | ||
eternal false | ||
overflowToDisk true | ||
maxElementsInMemory 10000 | ||
maxElementsOnDisk 10000000 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* Copyright 2012 SpringSource. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package grails.plugin.cache | ||
|
||
import org.codehaus.groovy.grails.commons.GrailsApplication | ||
import org.springframework.context.ApplicationContext | ||
|
||
import grails.plugin.cache.CacheConfigArtefactHandler.CacheConfigGrailsClass | ||
import grails.util.Environment | ||
|
||
/** | ||
* @author Burt Beckwith | ||
*/ | ||
class ConfigLoader { | ||
|
||
static final int DEFAULT_ORDER = 1000 | ||
|
||
void reload(ApplicationContext ctx) { | ||
def application = ctx.grailsApplication | ||
List<ConfigObject> configs = loadOrderedConfigs(application) | ||
reload configs, ctx | ||
} | ||
|
||
void reload(List<ConfigObject> configs, ApplicationContext ctx) { | ||
|
||
// order doesn't matter in this impl, but in general process in reverse order so | ||
// lower order values have higher priority and can override previous settings | ||
def configuredCacheNames = [] as LinkedHashSet | ||
for (ListIterator<ConfigObject> iter = configs.listIterator(configs.size()); iter.hasPrevious(); ) { | ||
ConfigObject co = iter.previous() | ||
ConfigBuilder builder = new ConfigBuilder() | ||
def config = co.cacheConfig ?: co.config | ||
if (config instanceof Closure) { | ||
builder.parse config | ||
} | ||
configuredCacheNames.addAll builder.cacheNames | ||
} | ||
|
||
GrailsCacheManager cacheManager = ctx.grailsCacheManager | ||
|
||
for (String name in cacheManager.cacheNames) { | ||
if (!configuredCacheNames.contains(name)) { | ||
cacheManager.destroyCache name | ||
} | ||
} | ||
|
||
for (String cacheName in configuredCacheNames) { | ||
cacheManager.getCache cacheName | ||
} | ||
} | ||
|
||
List<ConfigObject> loadOrderedConfigs(GrailsApplication application) { | ||
ConfigSlurper slurper = new ConfigSlurper(Environment.current.name) | ||
|
||
List<ConfigObject> configs = [] | ||
def cacheConfig | ||
for (configClass in application.cacheConfigClasses) { | ||
def config = slurper.parse(configClass.clazz) | ||
cacheConfig = config.cacheConfig | ||
if ((cacheConfig instanceof Closure) && processConfig(config, configClass)) { | ||
configs << config | ||
} | ||
} | ||
|
||
cacheConfig = application.config.grails.cache | ||
if ((cacheConfig.config instanceof Closure) && processConfig(cacheConfig, null)) { | ||
configs << cacheConfig | ||
} | ||
|
||
sortConfigs configs | ||
|
||
configs | ||
} | ||
|
||
protected boolean processConfig(ConfigObject config, CacheConfigGrailsClass configClass) { | ||
def cacheConfig | ||
String sourceClassName | ||
|
||
if (configClass == null) { | ||
cacheConfig = config.config | ||
sourceClassName = 'Config' | ||
} | ||
else { | ||
cacheConfig = config.cacheConfig | ||
sourceClassName = configClass.clazz.name | ||
} | ||
|
||
if (cacheConfig instanceof Closure) { | ||
def order = config.order | ||
if (!(order instanceof Number)) { | ||
config.order = DEFAULT_ORDER | ||
} | ||
config._sourceClassName = sourceClassName | ||
return true | ||
} | ||
|
||
false | ||
} | ||
|
||
protected void sortConfigs(List<Closure> configs) { | ||
configs.sort { c1, c2 -> | ||
c1.order == c2.order ? c1._sourceClassName <=> c2._sourceClassName : c1.order <=> c2.order | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/java/grails/plugin/cache/CacheConfigArtefactHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* Copyright 2012 SpringSource. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package grails.plugin.cache; | ||
|
||
import groovy.lang.GroovySystem; | ||
import groovy.lang.MetaClass; | ||
|
||
import org.codehaus.groovy.grails.commons.AbstractInjectableGrailsClass; | ||
import org.codehaus.groovy.grails.commons.ArtefactHandlerAdapter; | ||
import org.codehaus.groovy.grails.commons.InjectableGrailsClass; | ||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
|
||
/** | ||
* Artefact handler for CacheConfig classes. | ||
* | ||
* @author Burt Beckwith | ||
*/ | ||
public class CacheConfigArtefactHandler extends ArtefactHandlerAdapter { | ||
|
||
/** The artefact type. */ | ||
public static final String TYPE = "CacheConfig"; | ||
|
||
/** | ||
* Default constructor. | ||
*/ | ||
public CacheConfigArtefactHandler() { | ||
super(TYPE, CacheConfigGrailsClass.class, DefaultCacheConfigGrailsClass.class, TYPE); | ||
} | ||
|
||
/** | ||
* GrailsClass interface for CacheConfig definitions. | ||
*/ | ||
public static interface CacheConfigGrailsClass extends InjectableGrailsClass { | ||
// no methods | ||
} | ||
|
||
/** | ||
* Default implementation of <code>CacheConfigGrailsClass</code>. | ||
*/ | ||
public static class DefaultCacheConfigGrailsClass extends AbstractInjectableGrailsClass | ||
implements CacheConfigGrailsClass { | ||
|
||
/** | ||
* Default constructor. | ||
* @param wrappedClass | ||
*/ | ||
public DefaultCacheConfigGrailsClass(Class<?> wrappedClass) { | ||
super(wrappedClass, CacheConfigArtefactHandler.TYPE); | ||
} | ||
|
||
@Override | ||
public MetaClass getMetaClass() { | ||
// Workaround for http://jira.codehaus.org/browse/GRAILS-4542 | ||
return GroovySystem.getMetaClassRegistry().getMetaClass(DefaultCacheConfigGrailsClass.class); | ||
} | ||
|
||
@Override | ||
public Object newInstance() { | ||
Object instance = super.newInstance(); | ||
autowireBeanProperties(instance); | ||
return instance; | ||
} | ||
|
||
protected void autowireBeanProperties(Object instance) { | ||
ConfigurableApplicationContext ctx = (ConfigurableApplicationContext)grailsApplication.getMainContext(); | ||
ctx.getBeanFactory().autowireBeanProperties(instance, | ||
AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* Copyright 2012 SpringSource. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package grails.plugin.cache; | ||
|
||
import org.springframework.cache.CacheManager; | ||
|
||
/** | ||
* @author Burt Beckwith | ||
*/ | ||
public interface GrailsCacheManager extends CacheManager { | ||
|
||
boolean cacheExists(String name); | ||
|
||
boolean destroyCache(String name); | ||
} |
Oops, something went wrong.