TreeGateway / tree-gateway

This is a full featured and free API Gateway
http://treegateway.com
MIT License
189 stars 42 forks source link

Different cache rules to multiple groups #182

Open rodolfovilaca opened 5 years ago

rodolfovilaca commented 5 years ago

I don't know if it is a BUG or Enhancement feature.

Problem:

Today i have some services that i want to support different cache rules to different routes to my service, but as it is today in sortCaches function if i have different cache rules with groups it logs errors and return 0 cache rules to be applied. https://github.com/TreeGateway/tree-gateway/blob/a1ed94e289a8f225e3e140d72c84c58084b5b902/src/pipeline/cache/cache.ts#L110.

It doesn't allow me for example to do as follows:

name: 'My Service'
...
path: /my-service/
group:
    -
        id: TwoMinuteCache
        member:
            -
                method:
                    - GET
                path:
                    - get-something
    -
        id: TenMinuteCache
        member:
            -
                method:
                    - GET
                path:
                    - get-anotherthing
    -
        id: GeneralGroup
        member:
            -
                method:
                    - POST
                    - GET
                    - PUT
                    - DELETE
                    - OPTIONS
                protocol:
                    - http
                path:
                    - '!(get-something|get-anotherthing)'
proxy:
    target:
        host: 'http://172.18.0.1:port'
    timeout: '1 minute'
cache:
    -
        server:
            cacheTime: 10 minutes
            binary: true
        group:
            - TenMinuteCache
    -
        server:
            cacheTime: 2 minutes
            binary: true
        group:
            - TwoMinuteCache
            - GeneralGroup
throttling:
    -
        timeWindow: '1 minute'
        max: 100
authentication:
    strategy:
        name: jwt
        options:
            ...
    group:
        - GeneralGroup
        - TwoMinuteCache
        - TenMinuteCache
interceptor:
    request:
        -
            middleware:
                name: some-middleware

With this type of config today, it returns 0 cache rules and the gateway doesn't cache anything into redis.

As I said, i don't know if this is a intended behaviour or it is a BUG.

Solution

To support different cache rules to distinct paths we could do:

I'm currently forking and posting my PR with the last option implemented, but we should discuss the best option to go from here.