Coder Social home page Coder Social logo

aiohttp-cache's Introduction

A 2 MINUTES INTRODUCTION

Since I was child I loved technology. But I hadn't a computer until I was 15. I learned to program at my neighbourhood library with paper, a pen, and the C reference book. In my career, I was in the offensive and defensive part of the development. I destroyed and created software and systems and worked on hundreds of technologies, sometimes acting as CTO and others ones as CISO.

CAREER SUMMARY

I started my career as an ethical hacker and moved to the defender's side, little by little.

I have published more than 100 security tools and projects as Open Source. I did dozens of talks worldwide and wrote a networking security book (I'm writing another one).

I worked for top companies in different sectors: Startups, Insurance, Construction Companies, Software Companies, Banks, Governments, Telecommunications, Lawyers, or Internet Service Providers.

I am an expert in Python, REST API Security, and the security life cycle of software. I also have extensive experience and knowledge in software scalability, SecDevOps, automation, architecture design, or Cloud (AWS or Google Cloud, among others).

You can find more details of my career on my LinkedIn.

aiohttp-cache's People

Contributors

baldur avatar cr0hn avatar dependabot[bot] avatar evgenypopov72 avatar hlibowski avatar ikalnytskyi avatar pbabics avatar selutario avatar tambel avatar vitalyserhienko avatar wowkin2 avatar zhukovgreen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

aiohttp-cache's Issues

Bug when using custom middlewares

Hello,

I found an unexpected behaviour under this circumstance. I detail it below.

Description

When using aiohttp, if I add extra middlewares like in this example

app.add_api('spec.yaml',
            arguments={'title': 'Wazuh API',
                       'protocol': 'https' if configuration['https']['enabled'] else 'http',
                       'host': configuration['host'],
                       'port': configuration['port']
                       },
            strict_validation=True,
            validate_responses=True,
            pass_context_arg_name='request',
            options={"middlewares": [set_user_name, check_experimental(experimental)]})

aiohttp_cache stop working correctly. I'd like to note that some useful libraries like connexion adds its own middlewares, so maybe it is important to consider a fix for this.

I think I was able to discover the origin of the problem. The handler generated when calling the function decorated with @cache is itself wrapped by other handlers created in each extra middlewares. Below is an example image:
imageedit_2_8012954543

Fix

It would be necessary to look for these attributes recursively or to devise a different way in which to control the cache variables. Something like this works in my tests, although it is only a suggestion and I have not been able to dedicate much time to it.

def get_original_handler(handler):
    if hasattr(handler, 'cache_enable'):
        return handler
    elif hasattr(handler, 'keywords'):
        return get_original_handler(handler.keywords['handler'])


@web.middleware
async def cache_middleware(request, handler):

    original_handler = get_original_handler(handler)

    if getattr(original_handler, "cache_enable", False):
        ...

I hope you find this useful.

Kind regards,
Selu.

backend_config option is missing in the documentation

In the Custom Bandend / Redis, the following example code is shown.

As is:

redis_config = RedisConfig(db=4,
key_prefix="my_example")
setup_cache(app, cache_type="redis")

To be:

redis_config = RedisConfig(db=4,
key_prefix="my_example")
setup_cache(app, cache_type="redis", backend_config=redis_config)

I think backend_config option is missing in calling setup_cache, though it might set-up with the default option without error.

Sungsoo Kim

[Notification] aioredis broken

Thanks for the great work of this extension and I just want to let you know that when running on later Python versions, the following issue 1273 from aioredis appears which is related to the get_running_loop change of Python...

In the end, I just want to notify you regarding the move of aioredis into redis-py → issue 1225 which should happen by the end of February 2022 according issue 1273.

AttributeError: 'RedisConnection' object has no attribute 'get'

Hello,

when running the aiohttp app I have the following error:

File "/usr/local/lib/python3.7/site-packages/aiohttp_cache/backends.py", line 117, in get
redis_value = await redis.get(self.key_prefix + key)
AttributeError: 'RedisConnection' object has no attribute 'get'

NO-CACHE support

Could it be possible to provide a dummy cache backend that does nothing ?

I’m in need of a common interface for my client with or without cache (for instance with disabled) so i’ll need to implement my own no cache backend.

Release 1.1.0

Hi, I was trying to use this library but I noticed that version 1.1.0 was not published in pip. Consider publishing it 👍

Import error

When I run
from aiohttp_cache import cache, setup_cache
I get this error
ImportError: No module named 'aiohttp.web_reqrep'

Does the module aiohttp.web_reqrep exist?
PS: it is in file middleware.py

Add a possibility to build a custom key

Hello @cr0hn,

my team needs to have control over the key creation here:
https://github.com/cr0hn/aiohttp-cache/blob/master/aiohttp_cache/backends.py#L34

I think to do a little MR here to provide this possibility, but before, I want to check with you if conceptually the idea sounds good to you.

I would like:

  • modify the constructor of the BaseCache by adding there a cache key string with the default =
"{method}#{host}#{path}#{postdata}#{ctype}"
  • add helping method to form the key from the cache key str and request object
  • encrypt the key with token stored in .env

What do you think?

newbie question

Hi

I apologize for putting in a git issue, but curious if this is possible.

Could I ever POST JSON data to an aiohttp route, and then "cache" the JSON data to be used on a seperate GET route?

Hopefully this makes sense, I apologize I am still learning.. Like for example I am experimenting with POSTing a "user name", can this same "user name" be retrieved on the GET route?

from aiohttp import web


async def handle(request):
    response_obj = { 'status' : 'success' }

    # USE CACHED VALUE?
    return web.json_response(response_obj)


async def new_user(request):
    try:
        user = await request.json()
        print(user['name'])
        info = f"Creating new user with name: {user['name']}"
        print(info)

        # CAN I CACHE THIS response_obj?
        response_obj = { 'status' : 'success', 'info': info}
        return web.json_response(response_obj, status=200)
    except Exception as e:
        response_obj = { 'status' : 'failed', 'reason': str(e) }
        return web.json_response(response_obj, status=500)


app = web.Application()
app.router.add_get('/', handle)
app.router.add_post('/user', new_user)

web.run_app(app)

TypeError: can only join an iterable

Traceback (most recent call last):
  File "/venv/lib/python3.6/site-packages/aiohttp/web_protocol.py", line 378, in start
    resp = await self._request_handler(request)
  File "/venv/lib/python3.6/site-packages/aiohttp/web_app.py", line 341, in _handle
    resp = await handler(request)
  File "/venv/lib/python3.6/site-packages/aiohttp/web_middlewares.py", line 88, in impl
    return await handler(request)
  File "/venv/lib/python3.6/site-packages/aiohttp_cache/middleware.py", line 16, in middleware_handler
    key = cache_backend.make_key(request)
  File "/venv/lib/python3.6/site-packages/aiohttp_cache/backends.py", line 38, in make_key
    postdata="".join(request.post()),
TypeError: can only join an iterable

Found a bug will do a PR

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.