---
title: "Velocity模板缓存配置"
author: "mini_hu"
date: "2011-03-15T15:30:41Z"
tags: []
categories: ["编程语言"]
slug: "velocity"
draft: false
lastmod: "2011-03-15T15:30:41Z"
id: 963084
url: "https://pwind.iteye.com/blog/963084"
---

velocity开启缓存，主要用于线上环境，在velocity.properties中

```java
file.resource.loader.cache = true
file.resource.loader.modificationCheckInterval = -1

```


拨开读取模板代码：


```java
Template t = getTemplate(stack, velocityManager.getVelocityEngine(), invocation, finalLocation, encoding);

VelocityResult.java :
protected Template getTemplate(ValueStack stack, VelocityEngine velocity, ActionInvocation invocation, String location, String encoding) throws Exception {
        if (!location.startsWith("/")) {
            location = invocation.getProxy().getNamespace() + "/" + location;
        }

        Template template = velocity.getTemplate(location, encoding);

        return template;
    }

VelocityEngine.java
public Template getTemplate(String name, String encoding)
        throws ResourceNotFoundException, ParseErrorException, Exception
    {
        return ri.getTemplate( name, encoding );
    }

RuntimeInstance.java
public Template getTemplate(String name, String  encoding)
        throws ResourceNotFoundException, ParseErrorException, Exception
    {
        requireInitialization();

        return (Template)
                resourceManager.getResource(name,
                    ResourceManager.RESOURCE_TEMPLATE, encoding);
    }

ResourceManagerImpl.java
public Resource getResource(final String resourceName, final int resourceType, final String encoding)
        throws ResourceNotFoundException,
            ParseErrorException,
            Exception
    {
        String resourceKey = resourceType + resourceName;
        Resource resource = globalCache.get(resourceKey);

ResourceCacheImpl.java
public Resource get( Object key )
    {
        return (Resource) cache.get( key );
    }

```