| 902 | return lm_attr |
| 903 | |
| 904 | def fn(requests): |
| 905 | res = [] |
| 906 | remaining_reqs = [] |
| 907 | |
| 908 | # figure out which ones are cached and which ones are new |
| 909 | for req in requests: |
| 910 | hsh = hash_args(attr, req) |
| 911 | if hsh in self.dbdict: |
| 912 | ob = self.dbdict[hsh] |
| 913 | |
| 914 | assert ob is not None |
| 915 | |
| 916 | res.append(ob) |
| 917 | else: |
| 918 | res.append(None) |
| 919 | remaining_reqs.append(req) |
| 920 | |
| 921 | # actually run the LM on the requests that do not have cached results |
| 922 | rem_res = getattr(self.lm, attr)(remaining_reqs) |
| 923 | |
| 924 | # stick the new ones back into the list and also cache any of the new ones |
| 925 | resptr = 0 |
| 926 | for req, r in zip(remaining_reqs, rem_res): |
| 927 | while res[resptr] is not None: |
| 928 | resptr += 1 |
| 929 | |
| 930 | res[resptr] = r |
| 931 | |
| 932 | # caching |
| 933 | hsh = hash_args(attr, req) |
| 934 | self.dbdict[hsh] = r |
| 935 | self.dbdict.commit() |
| 936 | |
| 937 | return res |
| 938 | |
| 939 | return fn |
| 940 | |