{"id":21455675,"url":"https://github.com/skyl/djredis","last_synced_at":"2025-07-14T23:32:51.035Z","repository":{"id":916496,"uuid":"678456","full_name":"skyl/djredis","owner":"skyl","description":"A mixin that allows the user to easily add redis(h) attrs to django model instances and classes","archived":false,"fork":false,"pushed_at":"2011-03-01T01:10:10.000Z","size":224,"stargazers_count":13,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-16T06:57:04.812Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/skyl.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-05-21T05:32:23.000Z","updated_at":"2024-04-16T06:57:04.813Z","dependencies_parsed_at":"2022-07-15T05:16:49.134Z","dependency_job_id":null,"html_url":"https://github.com/skyl/djredis","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skyl%2Fdjredis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skyl%2Fdjredis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skyl%2Fdjredis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skyl%2Fdjredis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skyl","download_url":"https://codeload.github.com/skyl/djredis/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226002995,"owners_count":17558157,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-23T05:13:05.626Z","updated_at":"2024-11-23T05:13:06.223Z","avatar_url":"https://github.com/skyl.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Add redis fields to your django models.\n\nRequirements\n============\n\ndjredis requires redis-server, redis-py and redish.\n\nhttp://github.com/antirez/redis\n\nhttp://github.com/andymccurdy/redis-py\n\nI've added a couple of types to the main redish, hopefully we can get these\nput into:\nhttp://github.com/ask/redish\n\nFor right now, we have to run djredish with my fork:\nhttp://github.com/skyl/redish.git\n\n\nThe DredisMixin Class\n=====================\n\nUsing the DredisMixin is shown below.  Optionally add a method, ``redis_key`` to your modelclass\nthat returns the unique keyspace for the instance.\n\n::\n\n    from djredis.models import DredisMixin\n    import djredis.models\n\n    class Blog(models.Model, DredisMixin): # inherit from the mixin class\n        # use django's normal models stuff\n        author = models.ForeignKey('Author')\n        title = models.CharField(max_length=200)\n        \n        # add some redis fields to the model instances declaratively:\n        mycounter = djredis.models.Incr()\n        mystring = djredis.models.String()\n        myobject = djredis.models.Object()\n        mydict = djredis.models.Dict()\n        mylist = djredis.models.List()\n        myset = djredis.models.Set()\n        myzset = djredis.models.Zset()\n     \n      # optionally add a unique keyspace for the instance - default is shown below\n      def redis_key(self):\n          return '%s:%s:%s' % (self._meta.app_label, self._meta.module_name, self.pk)\n\n    # add table-level redis fields to the class\n    Blog.add_incr_to_class('classincr')\n    Blog.add_string_to_class('classstring')\n    Blog.add_object_to_class('classobject')\n    Blog.add_dict_to_class('classdict')\n    Blog.add_list_to_class('classlist')\n    Blog.add_set_to_class('classset')\n    Blog.add_zset_to_class('classzset')\n\n``djredis.models.X()`` is a data descriptor instance that has `__get__`, `__set__` and\n`__delete__` defined.  So, we can now grab a Blog instance and get and set the\nattributes that we created in our model definition.\n\n``djredis.models.Incr()``, a descriptor for redish.types.Incr::\n\n    \u003e\u003e\u003e b = Blog.objects.get(...) # get a blog instance\n    \u003e\u003e\u003e b.mycounter\n    0\n    \u003e\u003e\u003e type(b.mycounter)\n    \u003cclass 'redish.types.Incr'\u003e\n    \u003e\u003e\u003e b.mycounter.incr(5)\n    5\n    \u003e\u003e\u003e b.mycounter.decr(1)\n    4\n    \u003e\u003e\u003e del(b.mycounter)\n    \u003e\u003e\u003e b.mycounter\n    0\n    \u003e\u003e\u003e b.mycounter = 10\n    \u003e\u003e\u003e type(b.mycounter)\n    \u003cclass 'redish.types.Incr'\u003e\n    \n``djredis.models.String()``, a descriptor for redish.types.String::\n\n    \u003e\u003e\u003e b.mystring\n    ''\n    \u003e\u003e\u003e type(b.mystring)\n    \u003cclass 'redish.types.String'\u003e\n    \u003e\u003e\u003e b.mystring = 'foobar'\n    \u003e\u003e\u003e b.mystring.getset('bar')\n    'foobar'\n    \u003e\u003e\u003e b.mystring\n    'bar'\n    \u003e\u003e\u003e del(b.mystring)\n    \u003e\u003e\u003e type(b.mystring)\n    \u003cclass 'redish.types.String'\u003e\n\n``djredis.models.Object()``, a descriptor for redish.types.Object, able to\nstore any picklable python objects::\n\n    \u003e\u003e\u003e b.myobject\n    None\n    \u003e\u003e\u003e type(b.myobject)\n    \u003cclass 'redish.types.Object'\u003e\n    \u003e\u003e\u003e b.myobject = int\n    \u003e\u003e\u003e b.myobject\n    \u003ctype 'int'\u003e\n    \u003e\u003e\u003e b.myobject.getset({})\n    \u003ctype 'int'\u003e\n    \u003e\u003e\u003e b.myobject\n    {}\n    \u003e\u003e\u003e type(b.myobject)\n    \u003cclass 'redish.types.Object'\u003e\n    \u003e\u003e\u003e del(b.myobject)\n    \u003e\u003e\u003e b.myobject\n    None\n\n``Dict``, ``List``, ``Set`` and ``Zset`` work similarly.  They are also data\ndescriptors that allow\naccess to their respective ``redish.types`` and should be documented as the \nunderlying apis stabilize.\n\nNote that you should not use the descriptors on unsaved model instances.\nThe pk is used in building the redis key.  So, setting and accessing\nthe attributes on an unsaved model instance will save the data in redis under say,\n``content:blog:None:mylist`` instead of say ``content:blog:53:mylist``::\n\n    \u003e\u003e\u003e b = Blog()\n    \u003e\u003e\u003e b.mylist.append('bar')\n    \u003e\u003e\u003e b.redis_keys()\n    ['content:blog:None:mylist']\n    \u003e\u003e\u003e b.mylist.name\n    'content:blog:None:mylist'\n\nThis behavior serves no apparent purpose and will probably be changed.\n\n\n\nTable-level fields\n~~~~~~~~~~~~~~~~~~\n\nRedis types can also be added as attributes of the class using classmethods.\nThe attributes on the class that are created by the calls to the class methods\nare not descriptors however.  Therefore, one must be careful not to try to\nuse set or delete these attributes.  Setting these attributes directly is not\nsupported.  One may clear the value in redis by calling ``MyModel.myname_delete()``.\n\n``add_incr_to_class``.  After MyModel inherits from the mixin::\n\n    MyModel.add_incr_to_class('classincr')\n    MyModel.classincr # the redish.types.Incr object\n    MyModel.classincr.incr() # adds 1\n    MyModel.classincr.decr() # subtracts 1\n    # delete the value in the db\n    MyModel.classincr_delete()\n    \n``add_string_to_class``.  This is for adding an unpickled string field to your ModelClass::\n\n    MyModel.add_string_to_class('foostring')\n    MyModel.foostring # the redish.types.String object\n    MyModel.foostring_delete()\n    # more to come\n\n``add_object_to_class``.  For adding a pickled object to you ModelClass::\n\n    MyModel.add_object_to_class('classobject')\n    MyModel.classobject # the redish.types.Object object\n    MyModel.classobject_set(obj) # stores obj\n    MyModel.classobject_getset(obj) # returns the stored object and sets the value to obj\n    MyModel.classobject_delete() # remove the k/v from the db\n\nThe following methods create callables that return redish objects.\nSee the redish docs for more on how to interact with them.\n\n``add_list_to_class``.  Creates a callable on the class that returns a\nredish.types.List::\n\n    MyModel.add_list_to_class('classlist')\n    MyModel.classlist # the redish.types.List object\n    MyModel.classlist.appendleft('foo') #appends the string to the head of the list\n    MyModel.classlist.popleft() # returns 'foo' and removes it from the db\n    MyModel.classlist_delete() # remove the k/v from redis\n\n``add_dict_to_class``.  Creates a callable on the class that returns a\nredish.types.Dict::\n\n    MyModel.add_dict_to_class('classdict')\n    MyModel.classdict() # the redish.types.Dict object\n    MyModel.classdict_delete()\n\n``add_set_to_class``.  Creates a callable on the class that returns a\nredish.types.Set::\n\n    MyModel.add_set_to_class('classset')\n    MyModel.classset # the redish.types.Set object\n    MyModel.classset_delete()\n\n``add_zset_to_class``.  Creates a callable on the class that returns a\nredish.types.SortedSet::\n\n    MyModels.add_zset_to_class('classzset')\n    MyModel.classzset() # the redish.types.SortedSet object\n\n\nBase methods\n~~~~~~~~~~~~\n\nSome methods are added to your class and instances by using the mixin.\nThese methods are added without further action.\n\nInstance methods\n----------------\n\n``redis_key``.  Returns the unique key for the instance.\n\n``redis_keys``.  Returns the existing keys in the db for the instance.\n\n``redis_items``.  Returns the items (list of (k,v) pairs\n\nClass methods\n-------------\n\n``redis_base``.  Returns the unique key for the modelclass.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskyl%2Fdjredis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskyl%2Fdjredis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskyl%2Fdjredis/lists"}