예제 #1
0
파일: cache.py 프로젝트: syngraph/web3.py
        def middleware(method, params):
            lock_acquired = lock.acquire(blocking=False)

            try:
                if lock_acquired and method in rpc_whitelist:
                    cache_key = generate_cache_key((method, params))
                    if cache_key in cache:
                        # check that the cached response is not expired.
                        cached_at, cached_response = cache[cache_key]
                        cached_for = time.time() - cached_at

                        if cached_for <= cache_expire_seconds:
                            return cached_response
                        else:
                            del cache[cache_key]

                    # cache either missed or expired so make the request.
                    response = make_request(method, params)

                    if should_cache_fn(method, params, response):
                        cache[cache_key] = (time.time(), response)

                    return response
                else:
                    return make_request(method, params)
            finally:
                if lock_acquired:
                    lock.release()
예제 #2
0
파일: cache.py 프로젝트: drummonda/pluto
        def middleware(method, params):
            lock_acquired = lock.acquire(blocking=False)

            try:
                should_try_cache = (
                    lock_acquired and
                    method in rpc_whitelist and
                    not _is_latest_block_number_request(method, params)
                )
                if should_try_cache:
                    _update_block_info_cache()
                    latest_block_hash = block_info['latest_block']['hash']
                    cache_key = generate_cache_key((latest_block_hash, method, params))
                    if cache_key in cache:
                        return cache[cache_key]

                    response = make_request(method, params)
                    if should_cache_fn(method, params, response):
                        cache[cache_key] = response
                    return response
                else:
                    return make_request(method, params)
            finally:
                if lock_acquired:
                    lock.release()
 def cache_class():
     return {
         generate_cache_key(('fake_endpoint', [1])): (
             time.time() - 10,
             'value-a',
         ),
     }
예제 #4
0
 def cache_class():
     return {
         generate_cache_key(('fake_endpoint', [1])): (
             time.time(),
             {'result': 'value-a'},
         ),
     }
예제 #5
0
파일: cache.py 프로젝트: olisystems/web3.py
        def middleware(method, params):
            lock_acquired = lock.acquire(blocking=False)

            try:
                if lock_acquired and method in rpc_whitelist:
                    cache_key = generate_cache_key((method, params))
                    if cache_key in cache:
                        # check that the cached response is not expired.
                        cached_at, cached_response = cache[cache_key]
                        cached_for = time.time() - cached_at

                        if cached_for <= cache_expire_seconds:
                            return cached_response
                        else:
                            del cache[cache_key]

                    # cache either missed or expired so make the request.
                    response = make_request(method, params)

                    if should_cache_fn(method, params, response):
                        cache[cache_key] = (time.time(), response)

                    return response
                else:
                    return make_request(method, params)
            finally:
                if lock_acquired:
                    lock.release()
 def cache_class():
     return {
         generate_cache_key(('fake_endpoint', [1])): (
             time.time() - 10,
             {'result': 'value-a'},
         ),
     }
예제 #7
0
def _get_session(*args, **kwargs):
    cache_key = generate_cache_key((args, kwargs))
    if cache_key not in _session_cache:
        session = requests.Session()
        session.mount('https://', HTTPAdapter(pool_maxsize=MAX_POOL_SIZE))
        session.mount('http://', HTTPAdapter(pool_maxsize=MAX_POOL_SIZE))
        _session_cache[cache_key] = session
    return _session_cache[cache_key]
예제 #8
0
 def middleware(method, params):
     if method in rpc_whitelist:
         cache_key = generate_cache_key((method, params))
         if cache_key not in cache:
             response = make_request(method, params)
             if should_cache_fn(method, params, response):
                 cache[cache_key] = response
             return response
         return cache[cache_key]
     else:
         return make_request(method, params)
def _get_session(*args, **kwargs):
    cache_key = generate_cache_key((args, kwargs))
    if cache_key not in _session_cache:
        # This is the main change from original Web3 `_get_session`
        session = requests.sessions.Session()
        session.mount(
            'http://',
            HTTPAdapter(pool_connections=25, pool_maxsize=25, pool_block=True))
        session.mount(
            'https://',
            HTTPAdapter(pool_connections=25, pool_maxsize=25, pool_block=True))
        _session_cache[cache_key] = session
    return _session_cache[cache_key]
예제 #10
0
        def middleware(method, params):
            if method in rpc_whitelist:
                _update_block_info_cache()
                latest_block_hash = block_info['latest_block']['hash']
                cache_key = generate_cache_key((latest_block_hash, method, params))
                if cache_key in cache:
                    return cache[cache_key]

                response = make_request(method, params)
                if should_cache_fn(method, params, response):
                    cache[cache_key] = response
                return response
            else:
                return make_request(method, params)
예제 #11
0
파일: cache.py 프로젝트: syngraph/web3.py
        def middleware(method, params):
            lock_acquired = lock.acquire(blocking=False)

            try:
                if lock_acquired and method in rpc_whitelist:
                    cache_key = generate_cache_key((method, params))
                    if cache_key not in cache:
                        response = make_request(method, params)
                        if should_cache_fn(method, params, response):
                            cache[cache_key] = response
                        return response
                    return cache[cache_key]
                else:
                    return make_request(method, params)
            finally:
                if lock_acquired:
                    lock.release()
예제 #12
0
파일: cache.py 프로젝트: olisystems/web3.py
        def middleware(method, params):
            lock_acquired = lock.acquire(blocking=False)

            try:
                if lock_acquired and method in rpc_whitelist:
                    cache_key = generate_cache_key((method, params))
                    if cache_key not in cache:
                        response = make_request(method, params)
                        if should_cache_fn(method, params, response):
                            cache[cache_key] = response
                        return response
                    return cache[cache_key]
                else:
                    return make_request(method, params)
            finally:
                if lock_acquired:
                    lock.release()
def test_key_generation_is_deterministic(value):
    left = recursive_shuffle_dict(value)
    right = recursive_shuffle_dict(value)
    left_key = generate_cache_key(left)
    right_key = generate_cache_key(right)
    assert left_key == right_key
 def cache_class():
     return {
         generate_cache_key(
             (current_block_hash, 'fake_endpoint', [1])
         ): {'result': 'value-a'},
     }
예제 #15
0
파일: request.py 프로젝트: ycdk/web3.py
def _get_session(*args, **kwargs):
    cache_key = generate_cache_key((args, kwargs))
    if cache_key not in _session_cache:
        _session_cache[cache_key] = requests.Session()
    return _session_cache[cache_key]
 def cache_class():
     return {
         generate_cache_key(('fake_endpoint', [1])): {'result': 'value-a'},
     }
예제 #17
0
 def cache_class():
     return {
         generate_cache_key(
             (current_block_hash, 'fake_endpoint', [1])
         ): {'result': 'value-a'},
     }
def test_key_generation_is_deterministic(value):
    left = recursive_shuffle_dict(value)
    right = recursive_shuffle_dict(value)
    left_key = generate_cache_key(left)
    right_key = generate_cache_key(right)
    assert left_key == right_key
예제 #19
0
def _get_session(*args, **kwargs):
    cache_key = generate_cache_key((args, kwargs))
    if cache_key not in _session_cache:
        _session_cache[cache_key] = requests.Session()
    return _session_cache[cache_key]
예제 #20
0
 def cache_class():
     return {
         generate_cache_key(('fake_endpoint', [1])): 'value-a',
     }