parseCache parses c returns the specified Cache implementation.
(c string)
| 155 | |
| 156 | // parseCache parses c returns the specified Cache implementation. |
| 157 | func parseCache(c string) (imageproxy.Cache, error) { |
| 158 | if c == "" { |
| 159 | return nil, nil |
| 160 | } |
| 161 | |
| 162 | if c == "memory" { |
| 163 | c = fmt.Sprintf("memory:%d", defaultMemorySize) |
| 164 | } |
| 165 | |
| 166 | u, err := url.Parse(c) |
| 167 | if err != nil { |
| 168 | return nil, fmt.Errorf("error parsing cache flag: %w", err) |
| 169 | } |
| 170 | |
| 171 | switch u.Scheme { |
| 172 | case "azure": |
| 173 | return azurestoragecache.New("", "", u.Host) |
| 174 | case "gcs": |
| 175 | return gcscache.New(u.Host, strings.TrimPrefix(u.Path, "/")) |
| 176 | case "memory": |
| 177 | return lruCache(u.Opaque) |
| 178 | case "redis": |
| 179 | conn, err := redis.DialURL(u.String(), redis.DialPassword(os.Getenv("REDIS_PASSWORD"))) |
| 180 | if err != nil { |
| 181 | return nil, err |
| 182 | } |
| 183 | return rediscache.NewWithClient(conn), nil |
| 184 | case "s3": |
| 185 | return s3cache.New(u.String()) |
| 186 | case "file": |
| 187 | return diskCache(u.Path), nil |
| 188 | default: |
| 189 | return diskCache(c), nil |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // lruCache creates an LRU Cache with the specified options of the form |
| 194 | // "maxSize:maxAge". maxSize is specified in megabytes, maxAge is a duration. |