Add API Framework Revel Source Files
[iec.git] / src / foundation / api / revel / cache / init.go
1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
4
5 package cache
6
7 import (
8         "strings"
9         "time"
10
11         "github.com/revel/revel"
12 )
13
14 var cacheLog = revel.RevelLog.New("section", "cache")
15
16 func init() {
17         revel.OnAppStart(func() {
18                 // Set the default expiration time.
19                 defaultExpiration := time.Hour // The default for the default is one hour.
20                 if expireStr, found := revel.Config.String("cache.expires"); found {
21                         var err error
22                         if defaultExpiration, err = time.ParseDuration(expireStr); err != nil {
23                                 cacheLog.Panic("Could not parse default cache expiration duration " + expireStr + ": " + err.Error())
24                         }
25                 }
26
27                 // make sure you aren't trying to use both memcached and redis
28                 if revel.Config.BoolDefault("cache.memcached", false) && revel.Config.BoolDefault("cache.redis", false) {
29                         cacheLog.Panic("You've configured both memcached and redis, please only include configuration for one cache!")
30                 }
31
32                 // Use memcached?
33                 if revel.Config.BoolDefault("cache.memcached", false) {
34                         hosts := strings.Split(revel.Config.StringDefault("cache.hosts", ""), ",")
35                         if len(hosts) == 0 {
36                                 cacheLog.Panic("Memcache enabled but no memcached hosts specified!")
37                         }
38
39                         Instance = NewMemcachedCache(hosts, defaultExpiration)
40                         return
41                 }
42
43                 // Use Redis (share same config as memcached)?
44                 if revel.Config.BoolDefault("cache.redis", false) {
45                         hosts := strings.Split(revel.Config.StringDefault("cache.hosts", ""), ",")
46                         if len(hosts) == 0 {
47                                 cacheLog.Panic("Redis enabled but no Redis hosts specified!")
48                         }
49                         if len(hosts) > 1 {
50                                 cacheLog.Panic("Redis currently only supports one host!")
51                         }
52                         password := revel.Config.StringDefault("cache.redis.password", "")
53                         Instance = NewRedisCache(hosts[0], password, defaultExpiration)
54                         return
55                 }
56
57                 // By default, use the in-memory cache.
58                 Instance = NewInMemoryCache(defaultExpiration)
59         })
60 }