Commits

Yuya Nishihara committed 1f7e36b

cache hgwebdir object for userdirs

Comments (0)

Files changed (1)

hgext/webuserdir.py

 def _wraphgwebdir(obj):
     origcls = obj.__class__
     class hgwebudir(obj.__class__):
+        _userwebdirs = {}
         def run_wsgi(self, req):
             virtual = req.env.get('PATH_INFO', '').strip('/')
             m = re.match(r'~([^/]+)', virtual)
                 return super(hgwebudir, self).run_wsgi(req)
 
             user = m.group(1)
+            if user not in self._userwebdirs:
+                try:
+                    self._userwebdirs[user] = self._newuserwebdir(user)
+                except NoUserDirError:
+                    # delegates 404 error
+                    return super(hgwebudir, self).run_wsgi(req)
+            return self._userwebdirs[user].run_wsgi(req)
+
+        def _newuserwebdir(self, user):
+            userdir = getuserdir(self.ui, user)
             try:
-                userdir = getuserdir(self.ui, user)
-            except NoUserDirError:
-                return super(hgwebudir, self).run_wsgi(req)  # delegates 404 error
-
-            try:
-                # TODO: cache repos/webdir for certain seconds like hgwebdir
                 repos = hgwebdir_mod.findrepos([('~' + user, os.path.join(userdir, '**'))])
             except OSError:
-                return super(hgwebudir, self).run_wsgi(req)  # delegates 404 error
-            return origcls(repos, baseui=self.ui).run_wsgi(req)
+                raise NoUserDirError
+            return origcls(repos, baseui=self.ui)
 
     obj.__class__ = hgwebudir