NanmiCoder/MediaCrawler · error · ValueError
Unknown cache type: {cache_type}
Error message
Unknown cache type: {cache_type} What it means
ValueError from CacheFactory.create_cache when cache_type is neither 'memory' nor 'redis'. The factory lazily imports ExpiringLocalCache for 'memory' and RedisCache for 'redis', and treats any other string as a programming/configuration error.
Source
Thrown at cache/cache_factory.py:49
"""
@staticmethod
def create_cache(cache_type: str, *args, **kwargs):
"""
Create cache object
:param cache_type: Cache type
:param args: Arguments
:param kwargs: Keyword arguments
:return:
"""
if cache_type == 'memory':
from .local_cache import ExpiringLocalCache
return ExpiringLocalCache(*args, **kwargs)
elif cache_type == 'redis':
from .redis_cache import RedisCache
return RedisCache()
else:
raise ValueError(f'Unknown cache type: {cache_type}')
View on GitHub (pinned to d6f7c5bb90)
Solutions
- Set cache_type to exactly 'memory' or 'redis' (lowercase) in config/base_config.py or wherever create_cache is called.
- If you need a new backend, add an elif branch mapping your type to its class before relying on it.
- Log the incoming cache_type at startup so typos surface immediately.
Example fix
# before
CacheFactory.create_cache('mem')
# after
CacheFactory.create_cache('memory') Defensive patterns
Strategy: type-guard
Validate before calling
from cache.cache_factory import CacheFactory
SUPPORTED = {'memory', 'redis'}
if cache_type not in SUPPORTED:
raise SystemExit(f'cache type must be one of {sorted(SUPPORTED)}, got {cache_type!r}')
cache = CacheFactory.create_cache(cache_type) Type guard
def is_supported_cache(t: str) -> bool:
return isinstance(t, str) and t in {'memory', 'redis'} Try / catch
try:
cache = CacheFactory.create_cache(cfg.CACHETYPE_STRING)
except ValueError:
logger.error('bad cache type; defaulting to memory')
cache = CacheFactory.create_cache('memory') Prevention
- Validate config values against the supported set at startup
- Use exact lowercase literals 'memory'/'redis'
- Add a unit test enumerating supported cache types so new branches stay in sync
When it happens
Trigger: Passing cache_type from config (e.g. CACHETYPE_STRING) with a typo like 'Memory', 'mem', 'local', or an empty string; adding a new cache backend (e.g. 'memcached') without extending the factory's if/elif chain.
Common situations: Editing the config file's cache type value; case mismatch between documented values and the code's exact lowercase literals; upgrading/downgrading versions where supported cache types changed.
Related errors
- Invalid media platform: {platform!r}. Supported: {supported}
- Unsupported database type: {db_type}
- [BilibiliLogin.begin] Invalid Login Type Currently only supp
- [DouYinLogin.begin] Invalid Login Type Currently only suppor
- Wrong time range, please check your start and end argument,
AI-assisted analysis of NanmiCoder/MediaCrawler@d6f7c5bb90 (2026-08-15).
Data as JSON: /api/errors/4e3f456ff3ed1d70.
Report an issue: GitHub.