`

oscache cron

阅读更多


What is a Cron Expression?

Many of you are probably already familiar with the unix cron program. For those that aren't, cron is a daemon process that allows users to execute commands or scripts automatically at user-configurable dates and times. The important part as far as OSCache is concerned is the cron expression syntax that allows users to dictate when commands should be executed - you can now use the same syntax to expire content in OSCache! A cron expression is a simple text string that specifies particular dates and/or times that are matched against.

How Does OSCache Match Against an Expression?

OSCache uses cron expressions in a manner that might seem 'backwards' to what you might initially expect. When using a cron expression to test if a cache entry is stale, OSCache finds the date and time (prior to the current time) that most recently matches the supplied expression. This date/time is used as the expiry time - entries that were placed in the cache prior to this expiry time are considered stale and result in a NeedsRefreshException being thrown.

As an example, suppose you specify a cron expiry that matches every hour, on the hour ("0 * * * *"). If the current time is 10:42pm, then any content that was placed in the cache prior to 10:00pm would be considered stale.

What is the Difference Between the Refresh Period and a Cron Expression?

The difference between the refresh period and a cron expression is that the refresh period specifies the maximum allowable age of a cache entry, whilst a cron expression specifies specific expiry times, regardless of how old an entry is. Eg imagine caching an object at 10:29am. With a refresh period of 30 minutes that entry would expire at 10:59am. With a cron expression of "0,30 * * * *" that entry would expire at 10:30am.

The Cron Expression Syntax

A cron expression consists of the following 5 fields:

  • Minute - specifies what minute of the hour to expire content on. It is a number between 0 and 59.
  • Hour - determines what hour of the day content will expire on. It is specified using the 24-hour clock, so the values must be between 0 (midnight) and 23 (11pm).
  • DOM - the Day of the Month. This is a number from 1 to 31. It indicates what day the content should expire on. For example, to expire content on the 10th of every month, set this field to 10.
  • Month - month of the year to expire the content. This can be specified either numerically (1 through 12), or by using the actual month name (eg 'January'). Month names are case-insensitive and only the first three characters are taken into account - the rest are ignored.
  • DOW - The Day of the Week that the content should be expired on. This can be a numeric value (0-6, where 0 = Sunday, 1 = Monday, ..., 6 = Saturday), or you can use the actual day name. As is the case with month names, DOW names are case-insensitive and only the first three characters matter.

If you don't want to specify a value for a particular field (ie you want the cron expression to match all values for that field), just use a * character for the field value.

As an example, an expression that expired content at 11:45pm each day during April would look like this: "45 23 * April *".

OSCache also allows you to optionally specify lists, ranges and intervals (or even a combination of all three) within each field:

  • Lists - items in a list are delimited using the ',' character. Content expiry times will be matched against all values in the list for that field. For example, "0,15,30,45 * * * *" will expire content every quarter-hour on the quarter hour.
  • Ranges - ranges are specified using the '-' character. A range will include all values from the start to the end value (inclusive). For example, "* * * Jan-June *" will expire content every minute only during the first 6 months of the year.
  • Intervals - an interval is specified using the '/' character. The value to the left of the '/' character indicates either the starting point or the range of values that should be incremented over, while the value to the right of the '/' specifies the interval or step size. Some examples -
    "10/20 * * * *" is equivalent to "10,30,50 * * * *", 
    while "10-45/20 * * * *" would only match 10 and 30 minutes past the hour, since 50 is outside the specified range.
    Supplying '*' as the left-hand value of an interval will match the same values as if you had specified a range over all possible values.
    Eg "*/10 * * * *" matches minutes 0,10,20,30,40 and 50.
    

To have a look at further examples of both valid and invalid syntax, it is suggested you take a look at the JUnit test cases in the com.opensymphony.oscache.util.TestFastCronParser class. This class is located under the src/core/test directory. For examples of how to specify cron expiry times using the taglibs.

Notes

  • You can specify both a cron expression and a refresh interval at the same time if you like. This is useful in cases where you always want to expire content at midnight, but you also never want it to be more than 6 hours old.
  • Specifying out of range values, such as a 13 in the month field, will cause a ParseException to be thrown.
  • If a DOM is specified that cannot exist given the allowable months, a ParseException will be thrown. For example, "* * 31 Feb *" will fail because no date will ever match the 31st February!
  • The DOM and DOW fields cannot both be specified at the same time. One must always be set to '*' otherwise a ParseException will be thrown.
  • Leap years and local daylight savings time are taken into account. Eg "0 0 29 Feb *" will match midnight on the 29th February, ie only once every 4 years.
  • Currently the time used to match the cron expression against is always based on the local time on the server. If there is demand support for specifying an alternate timezone may 
分享到:
评论

相关推荐

    OSCache学习例子 实例

    OSCache是开源的Java缓存解决方案,主要用于提高Web应用程序的性能和响应速度。它是由OpenSymphony团队开发的,能够缓存对象、SQL查询结果甚至整个页面,避免了频繁访问数据库或执行昂贵的计算,从而降低了系统负载...

    oscache-2.1.jar

    oscache-2.1.jar oscache-2.1.jar

    oscache缓存配置

    《osCache缓存配置详解》 osCache是Java平台上的一个高效、易用的缓存解决方案,它由Tuckey组织开发,广泛应用于各种Web应用中,以提高数据读取速度,减轻数据库压力。osCache的核心功能是提供了一个内存中的对象...

    Oscache框架的搭建步骤

    ### Oscache框架的搭建与应用详解 在现代Web开发中,缓存技术是提升系统响应速度、优化用户体验的关键策略之一。Oscache框架作为一种高效、灵活的缓存解决方案,在Java Web应用,尤其是JSP环境中,提供了强大的缓存...

    oscache缓存技术应用

    **oscache缓存技术应用** **一、OSCache简介与应用** Cache技术是提升系统响应速度和性能的关键手段,尤其在Web应用中,通过缓存页面的输出结果,能够显著提高系统性能。OSCache是由OpenSymphony开发的一种创新...

    oscache缓存技术入门实例

    osCache 是一个开源的、基于Java的缓存框架,它为Java应用程序提供了高效且可配置的内存缓存功能。在本文中,我们将深入探讨osCache的基本概念、工作原理以及如何在实际项目中进行配置和使用。 一、osCache基本概念...

    OSCache需要的包

    OSCache 是一个高效的、开源的缓存框架,主要用于 Java 应用程序,它提供了一种在内存中存储对象的方式,以提高数据访问速度并减轻数据库的负载。在Java Web开发中,OSCache常被用于实现Session复制和分布式缓存,...

    oscache说明

    - `cron`:使用 cron 表达式定义缓存内容何时过期。 - `refresh`:如果设置为 `true`,则在每次请求时都会检查缓存是否已过期,即使未达到预设的 `time` 或 `duration`。 **缓存策略** OSCache 提供了多种缓存策略...

    oscache详细配置文档

    OSCache 是一个强大的开源缓存解决方案,主要用于提升 Java Web 应用程序的性能。它能够缓存页面内容,减轻数据库压力,并减少服务器的资源消耗。本文将详细介绍 OSCache 的配置和使用方法。 **一、缓存整个页面** ...

    OSCache配置说明文档

    本文档的主要目的是阐述OSCache的核心功能、配置方法以及实际应用中的操作步骤,帮助开发者理解和有效地利用OSCache提升应用程序性能。 阅读对象: 此文档适合对Java Web开发有一定了解,希望引入缓存机制以优化...

    oscache,缓存机制的使用

    此外,`oscache`还提供了诸如`cron`、`groups`、`language`、`refreshpolicyclass`、`refreshpolicyparam`等属性,进一步增强了缓存管理的灵活性和精细化控制能力。 ### 结论 通过合理配置和使用oscache,开发人员...

    oscache-2.4.1-full

    OSCache是OpenSymphony开发的一款高效、开源的Java缓存框架,主要应用于Web应用程序,特别是JSP环境。其核心功能是提供内存级别的缓存服务,从而显著提高网页的加载速度和减少数据库的压力。标题"oscache-2.4.1-full...

    oscache处理

    - 配置osCache插件:首先需要将osCache的jar包添加到项目类路径中,然后在struts.xml配置文件中引入osCache拦截器,并配置相应的拦截规则。 2. **iBatis与osCache**: - iBatis是一个轻量级的持久层框架,它允许...

    oscache-java缓存框架

    osCache是Java开发中常用的缓存框架之一,它主要用于提高应用程序的性能和效率,通过将数据存储在内存中,减少对数据库的访问。osCache不仅可以用于Web应用,也可以用于任何Java应用程序,支持集群环境,提供了丰富...

    oscache的使用实例和详解

    **osCache:高效缓存框架详解与实例应用** osCache是一款流行且强大的Java缓存框架,主要用于提高应用程序的性能和响应速度。它通过将经常访问的数据存储在内存中,避免了反复读取数据库或计算数据的过程,从而显著...

    OsCache缓存框架使用示例

    OsCache是Java应用程序中常用的缓存框架,它能够有效地提高应用程序的性能,通过将经常访问的数据存储在内存中,减少对数据库或其他数据源的访问,从而降低系统负载。本示例将通过一个天气预报Web服务的场景,详细...

    oscache(JSP定制标记应用)

    javaweb做页面缓存常用,OSCache是一个工业级的J2EE缓存实现。OSCache不但能缓存java对象,还可以缓存页面,http请求和二进制内容,例如pdf文件等。通过应用OSCache,我们不但可以实现通常的Cache功能,还能够改善...

    oscache缓存技术

    **osCache缓存技术详解** osCache是一款广泛应用于Java应用程序中的开源缓存解决方案,由OpenSymphony团队开发。它提供了一种高效、可扩展的方式来管理应用程序中的数据缓存,从而提高系统的性能和响应速度。...

    oscache-2.2jar包

    osCache是Java平台上的一个开源缓存解决方案,主要用于提高应用程序的性能和响应速度。oscache-2.2jar包是该软件的一个版本,版本号为2.2。它包含了一系列的类和接口,用于实现内存中的对象缓存,从而减少对数据库或...

    用OSCache进行缓存对象

    1、OSCache是什么? 2、OSCache的特点 3、有关“用OSCache进行缓存对象”的研究

Global site tag (gtag.js) - Google Analytics