Code Monkey home page Code Monkey logo

Comments (4)

vincentsarago avatar vincentsarago commented on August 16, 2024

We removed the cache! let's revisit later

from titiler.

vincentsarago avatar vincentsarago commented on August 16, 2024

I've started to work on a middleware using aiocache https://gist.github.com/vincentsarago/5579d2bf0c26c17439bbce0c0e432664

It doesn't seems to work on tile endpoint because the app return a streaming response ...

from titiler.

vincentsarago avatar vincentsarago commented on August 16, 2024

I got a cache working but I don't think the middleware will work.

"""Custom Cached."""
import aiocache
from starlette.responses import Response

class cached(aiocache.cached):
    """Custom Cached Decorator."""

    async def get_from_cache(self, key):
        """update headers."""
        try:
            value = await self.cache.get(key)
            if isinstance(value, Response):
                value.headers["X-Cache"] = "HIT"
            return value
        except Exception:
            aiocache.logger.exception("Couldn't retrieve %s, unexpected error", key)

    async def decorator(
        self, f, *args, cache_read=True, cache_write=True, aiocache_wait_for_write=True, **kwargs
    ):
        """add non-async compat."""
        key = self.get_cache_key(f, args, kwargs)

        if cache_read:
            value = await self.get_from_cache(key)
            if value is not None:
                return value

        if is_coroutine_callable(f):
            result = await f(*args, **kwargs)
        else:
            result = f(*args, **kwargs)

        if cache_write:
            if aiocache_wait_for_write:
                await self.set_in_cache(key, result)
            else:
                asyncio.ensure_future(self.set_in_cache(key, result))

        return result

Then I'm adding @cached() on all the endpoints I want to cache

Notes:

Natively Aiocache will only work on async endpoints (async def) which we don't use right know in TiTiler because all low level code is not async (Rasterio). To overcome I updated the decorator method with

        if is_coroutine_callable(f):
            result = await f(*args, **kwargs)
        else:
            result = f(*args, **kwargs)

I'm not sure we'll add this by default but at least we should document it

from titiler.

vincentsarago avatar vincentsarago commented on August 16, 2024

done in https://developmentseed.org/titiler/examples/code/tiler_with_cache/

from titiler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.