<div dir="ltr">Hi Ivan,<div><br></div><div>it feels what you suggest would work safely on for transactions set the serializable isolation level, not repeteable reads down to read uncommitted (since phantom reads could occur there, and the non-existing cache would hide new results).<br><div><br></div></div><div>Cheers</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 16, 2015 at 5:55 PM, Ivan Zakrevskyi <span dir="ltr"><<a href="mailto:ivan.zakrevskyi@rebelmouse.com" target="_blank">ivan.zakrevskyi@rebelmouse.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi, all. Thanks for answer. I'll try to explain.<br><br></div>Try to get existent object.<br><br>In [2]: store.get(StTwitterProfile, (1,3))<br>base.py:50 => <br>u'(0.001) SELECT ... FROM twitterprofile WHERE twitterprofile.context_id = %s AND twitterprofile.user_id = %s LIMIT 1; args=(1, 3)'<br>Out[2]: <users.orm.TwitterProfile at 0x7f1e93b6d450><br><br>In [3]: store.get(StTwitterProfile, (1,3))<br>Out[3]: <users.orm.TwitterProfile at 0x7f1e93b6d450><br><br>In [4]: store.get(StTwitterProfile, (1,3))<br>Out[4]: <users.orm.TwitterProfile at 0x7f1e93b6d450><br><div><div><div><div><div class="gmail_extra"><br clear="all"><div><div><div dir="ltr"><div><div dir="ltr"><div><div>You can see, that storm made only one query.<br><br></div><div>Ok, now try get nonexistent twitter profile for given context:<br><br>In [5]: store.get(StTwitterProfile, (10,3))<br>base.py:50 => <br>u'(0.001) SELECT ... FROM twitterprofile WHERE twitterprofile.context_id = %s AND twitterprofile.user_id = %s LIMIT 1; args=(1, 10)'<br><br>In [6]: store.get(StTwitterProfile, (10,3))<br>base.py:50 => <br>u'(0.001) SELECT ... FROM twitterprofile WHERE twitterprofile.context_id = %s AND twitterprofile.user_id = %s LIMIT 1; args=(1, 10)'<br><br>In [7]: store.get(StTwitterProfile, (10,3))<br>base.py:50 => <br>u'(0.001) SELECT ... FROM twitterprofile WHERE twitterprofile.context_id = %s AND twitterprofile.user_id = %s LIMIT 1; args=(1, 10)'<br><br></div><div>Storm sends a query to the database each time.<br><br></div><div>For example, we have a some util:<br><br></div><div>def myutil(user_id, *args, **kwargs):<br></div><div>    context_id = get_context_from_mongodb_redis_memcache_environment_etc(user_id, *args, **kwargs)<br></div><div>    twitter_profile = store.get(TwitterProfile, (context_id, user_id))<br></div><div>    return twitter_profile.some_attr<br><br></div><div>In this case, Storm will send a query to the database each time.<br><br>The similar situation for non-existent relation.<br><br>In [20]: u = store.get(StUser, 10)<br>base.py:50 => <br>u'(0.001) SELECT ... FROM user WHERE <a href="http://user.id" target="_blank">user.id</a> = %s LIMIT 1; args=(10,)'<br><br><br>In [22]: u.profile<br>base.py:50 => <br>u'(0.001) SELECT ... FROM userprofile WHERE userprofile.user_id = %s LIMIT 1; args=(10,)'<br><br>In [23]: u.profile<br>base.py:50 => <br>u'(0.001) SELECT ... FROM userprofile WHERE userprofile.user_id = %s LIMIT 1; args=(10,)'<br><br>In [24]: u.profile<br>base.py:50 => <br>u'(0.001) SELECT ... FROM userprofile WHERE userprofile.user_id = %s LIMIT 1; args=(10,)'<br><br></div><div>I've created a temporary patch, to reduce number of DB queries (see bellow). But I am sure that a solution can be more elegant (on library level).<br><br><br></div><div>class NonexistentCache(list):<br><br>    _size = 1000<br><br>    def add(self, val):<br>        if val in self:<br>            self.remove(val)<br>        self.insert(0, val)<br>        if len(self) > self._size:<br>            self.pop()<br><br><br>class Store(StoreOrig):<br><br>    def __init__(self, database, cache=None):<br>        StoreOrig.__init__(self, database, cache)<br>        self.nonexistent_cache = NonexistentCache()<br><br>    def get(self, cls, key, exists=False):<br>        """Get object of type cls with the given primary key from the database.<br><br>        This method is patched to cache nonexistent values to reduce number of DB-queries.<br>        If the object is alive the database won't be touched.<br><br>        @param cls: Class of the object to be retrieved.<br>        @param key: Primary key of object. May be a tuple for composed keys.<br><br>        @return: The object found with the given primary key, or None<br>            if no object is found.<br>        """<br><br>        if self._implicit_flush_block_count == 0:<br>            self.flush()<br><br>        if type(key) != tuple:<br>            key = (key,)<br><br>        cls_info = get_cls_info(cls)<br><br>        assert len(key) == len(cls_info.primary_key)<br><br>        primary_vars = []<br>        for column, variable in zip(cls_info.primary_key, key):<br>            if not isinstance(variable, Variable):<br>                variable = column.variable_factory(value=variable)<br>            primary_vars.append(variable)<br><br>        primary_values = tuple(var.get(to_db=True) for var in primary_vars)<br><br>        # Patched<br>        alive_key = (cls_info.cls, primary_values)<br>        obj_info = self._alive.get(alive_key)<br>        if obj_info is not None and not obj_info.get("invalidated"):<br>            return self._get_object(obj_info)<br><br>        if obj_info is None and not exists and alive_key in self.nonexistent_cache:<br>            return None<br>        # End of patch<br><br>        where = compare_columns(cls_info.primary_key, primary_vars)<br><br>        select = Select(cls_info.columns, where,<br>                        default_tables=cls_info.table, limit=1)<br><br>        result = self._connection.execute(select)<br>        values = result.get_one()<br>        if values is None:<br>            # Patched<br>            self.nonexistent_cache.add(alive_key)<br>            # End of patch<br>            return None<br>        return self._load_object(cls_info, result, values)<br><br>    def get_multi(self, cls, keys, exists=False):<br>        """Get objects of type cls with the given primary key from the database.<br><br>        If the object is alive the database won't be touched.<br><br>        @param cls: Class of the object to be retrieved.<br>        @param key: Collection of primary key of objects (that may be a tuple for composed keys).<br><br>        @return: The object found with the given primary key, or None<br>            if no object is found.<br>        """<br>        result = {}<br>        missing = {}<br>        if self._implicit_flush_block_count == 0:<br>            self.flush()<br><br>        for key in keys:<br>            key_orig = key<br>            if type(key) != tuple:<br>                key = (key,)<br><br>            cls_info = get_cls_info(cls)<br><br>            assert len(key) == len(cls_info.primary_key)<br><br>            primary_vars = []<br>            for column, variable in zip(cls_info.primary_key, key):<br>                if not isinstance(variable, Variable):<br>                    variable = column.variable_factory(value=variable)<br>                primary_vars.append(variable)<br><br>            primary_values = tuple(var.get(to_db=True) for var in primary_vars)<br><br>            alive_key = (cls_info.cls, primary_values)<br>            obj_info = self._alive.get(alive_key)<br>            if obj_info is not None and not obj_info.get("invalidated"):<br>                result[key_orig] = self._get_object(obj_info)<br>                continue<br><br>            if obj_info is None and not exists and alive_key in self.nonexistent_cache:<br>                result[key_orig] = None<br>                continue<br><br>            missing[primary_values] = key_orig<br><br>        if not missing:<br>            return result<br><br>        wheres = []<br>        for i, column in enumerate(cls_info.primary_key):<br>            wheres.append(In(cls_info.primary_key[i], tuple(v[i] for v in missing)))<br>        where = And(*wheres) if len(wheres) > 1 else wheres[0]<br><br>        for obj in self.find(cls, where):<br>            key_orig = missing.pop(tuple(var.get(to_db=True) for var in get_obj_info(obj).get("primary_vars")))<br>            result[key_orig] = obj<br><br>        for primary_values, key_orig in missing.items():<br>            self.nonexistent_cache.add((cls, primary_values))<br>            result[key_orig] = None<br><br>        return result<br><br>    def reset(self):<br>        StoreOrig.reset(self)<br>        del self.nonexistent_cache[:]<br></div><div><br></div><div><br></div></div></div></div></div></div></div><div><div class="h5">
<br><div class="gmail_quote">2015-01-16 9:03 GMT+02:00 Free Ekanayaka <span dir="ltr"><<a href="mailto:free@64studio.com" target="_blank">free@64studio.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi Ivan<div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Thu, Jan 15, 2015 at 10:23 PM, Ivan Zakrevskyi <span dir="ltr"><<a href="mailto:ivan.zakrevskyi@rebelmouse.com" target="_blank">ivan.zakrevskyi@rebelmouse.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><div>Hi all.<br><br></div>Storm has excellent caching behavior, but stores in Store._alive only existent objects. If object does not exists for some key, storm makes DB-query again and again.<br><br></div>Are you planning add caching for keys of nonexistent objects to prevent DB-query?<br></div></div></blockquote><div><br></div></div></div><div>If an object doesn't exist in the cache it meas that either it was not yet loaded at all,  or it was loaded but it's now mark as "invalidated" (for example the transaction in which it was loaded fresh has terminated).</div><div><br></div><div>So I'm note sure what you mean in you question, but I don't think anything more that could be cached (in terms of key->object values).</div><div><br></div><div>Cheers</div></div><br></div></div>
</blockquote></div><br></div></div></div></div></div></div></div></div>
<br>--<br>
storm mailing list<br>
<a href="mailto:storm@lists.canonical.com">storm@lists.canonical.com</a><br>
Modify settings or unsubscribe at: <a href="https://lists.ubuntu.com/mailman/listinfo/storm" target="_blank">https://lists.ubuntu.com/mailman/listinfo/storm</a><br>
<br></blockquote></div><br></div>