| 69 | } |
| 70 | |
| 71 | func NewAsyncCache(cfg config.Cache, maxExecutionTime time.Duration) (*AsyncCache, error) { |
| 72 | graceTime := time.Duration(cfg.GraceTime) |
| 73 | if graceTime > 0 { |
| 74 | log.Errorf("[DEPRECATED] detected grace time configuration %s. It will be removed in the new version", |
| 75 | graceTime) |
| 76 | } |
| 77 | if graceTime == 0 { |
| 78 | // Default grace time. |
| 79 | graceTime = maxExecutionTime |
| 80 | } |
| 81 | if graceTime < 0 { |
| 82 | // Disable protection from `dogpile effect`. |
| 83 | graceTime = 0 |
| 84 | } |
| 85 | |
| 86 | var cache Cache |
| 87 | var transaction TransactionRegistry |
| 88 | var err error |
| 89 | // transaction will be kept until we're sure there's no possible concurrent query running |
| 90 | transactionDeadline := 2 * graceTime |
| 91 | |
| 92 | switch cfg.Mode { |
| 93 | case "file_system": |
| 94 | cache, err = newFilesSystemCache(cfg, graceTime) |
| 95 | transaction = newInMemoryTransactionRegistry(transactionDeadline, transactionEndedTTL) |
| 96 | case "redis": |
| 97 | var redisClient redis.UniversalClient |
| 98 | redisClient, err = clients.NewRedisClient(cfg.Redis) |
| 99 | cache = newRedisCache(redisClient, cfg) |
| 100 | transaction = newRedisTransactionRegistry(redisClient, transactionDeadline, transactionEndedTTL) |
| 101 | default: |
| 102 | return nil, fmt.Errorf("unknown config mode") |
| 103 | } |
| 104 | |
| 105 | if err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | |
| 109 | maxPayloadSize := cfg.MaxPayloadSize |
| 110 | |
| 111 | return &AsyncCache{ |
| 112 | Cache: cache, |
| 113 | TransactionRegistry: transaction, |
| 114 | graceTime: graceTime, |
| 115 | MaxPayloadSize: maxPayloadSize, |
| 116 | SharedWithAllUsers: cfg.SharedWithAllUsers, |
| 117 | }, nil |
| 118 | } |