forked from chaosbotgroup/chaos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.py
33 lines (26 loc) · 1.03 KB
/
patch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Inject some memoize caching into our github api functions.
We're keeping the caching layer separate from the api layer by doing it this way.
"""
import inspect
import settings
from os.path import dirname, abspath, join
from functools import partial
from memoize import memoize
from memoize.backends import json_backend
import github_api.voting
import github_api.repos
import github_api.prs
def decorate(fn, dec):
"""helper for monkey-patch-decorating functions in different modules"""
mod = inspect.getmodule(fn)
new_fn = dec(fn)
setattr(mod, fn.__name__, new_fn)
cache_dir = join(dirname(abspath(__file__)), settings.MEMOIZE_CACHE_DIRNAME)
api_memoize = partial(memoize, blacklist={"api"}, backend=json_backend(cache_dir))
# now let's memoize some very frequent api calls that don't change often
decorate(github_api.voting.get_vote_weight, api_memoize("1d"))
decorate(github_api.repos.get_num_watchers, api_memoize("10m"))
decorate(github_api.prs.get_is_mergeable, api_memoize("2m"))