Skip to content

Commit

Permalink
Replace memcached with in-memory
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Dec 10, 2024
1 parent f2f42f4 commit 855ba7f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ uvicorn valis.wsgi:app --reload
```
This will start a local web server at `http://localhost:8000/valis/`. The API documentation will be located at `http://localhost:8000/valis/docs`. Or to see the alternate documentation, go to `http://localhost:8000/valis/redoc/`

By default, the app will try to cache some route responses to a Redis database in localhost. If you don't have a Redis instance running you can use `memcached` for testing. To do so, edit `~/.config/sdss/valis.yaml` and add `cache_backend: memcached`.
By default, the app will try to cache some route responses to a Redis database in localhost. If you don't have a Redis instance running you can use `in-memory` for testing (this caches the response directly in RAM). To do so, edit `~/.config/sdss/valis.yaml` and add `cache_backend: in-memory`.

### Database Connection

Expand Down Expand Up @@ -95,7 +95,7 @@ Additionally, you can set the environment variable `VALIS_DB_RESET=false` or add
This section describes a variety of deployment methods. Valis uses gunicorn as its
wsgi http server. It binds the app both to port 8000, and a unix socket. The default mode is to start valis with an awsgi uvicorn server, with 4 workers.

Valis requires a Redis database running at the default location in `localhost:6379`. If this is not possible, caching can be done in memory by modifying `~/.config/sdss/valis.yaml` to use `cache_backend: memcached`.
Valis requires a Redis database running at the default location in `localhost:6379`. If this is not possible, caching can be done in memory by modifying `~/.config/sdss/valis.yaml` to use `cache_backend: in-memory`.

### Deploying Zora + Valis together
See the SDSS [Zora+Valis Docker](https://github.com/sdss/zora_valis_dockers) repo page.
Expand Down
9 changes: 4 additions & 5 deletions python/valis/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
get_typed_signature
)
from fastapi_cache import FastAPICache
from fastapi_cache.backends.memcached import MemcachedBackend
from fastapi_cache.backends.inmemory import InMemoryBackend
from fastapi_cache.backends.redis import RedisBackend
from fastapi_cache.decorator import _augment_signature, _locate_param
from redis.asyncio.client import Redis
Expand Down Expand Up @@ -71,10 +71,9 @@
@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
backend = settings.cache_backend
if backend == 'memcached':
logger.info('Using Memcached backend for caching')
memcache_client = MemcacheClient('localhost', 11211)
FastAPICache.init(MemcachedBackend(memcache_client),
if backend == 'in-memory':
logger.info('Using in-memory backend for caching')
FastAPICache.init(InMemoryBackend(),
prefix="fastapi-cache",
key_builder=valis_cache_key_builder)
elif backend == 'redis':
Expand Down
4 changes: 2 additions & 2 deletions python/valis/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class EnvEnum(str, Enum):


class CacheBackendEnum(str, Enum):
memcached = 'memcached'
inmemory = 'in-memory'
redis = 'redis'


Expand All @@ -48,7 +48,7 @@ class Settings(BaseSettings):
db_host: Optional[str] = 'localhost'
db_pass: Optional[str] = None
db_reset: bool = True
cache_backend: CacheBackendEnum = CacheBackendEnum.redis
cache_backend: CacheBackendEnum = CacheBackendEnum.inmemory
model_config = SettingsConfigDict(env_prefix="valis_")

@field_validator('allow_origin')
Expand Down

0 comments on commit 855ba7f

Please sign in to comment.