title: How to add elements to a sorted redis set in python
body: <p>I have a Django app where users upload photos for the general public to see and comment on. One requirement is that the uploaded photo must not be something the website has already seen recently. To accomplish this, I calculate the <a href="https://www.safaribooksonline.com/blog/2013/11/26/image-hashing-with-python/" rel="nofollow">average (perceptual) hash</a> of every image presented for uploading, saving it in the DB. Later, when a new image is being put up, it's average hash is compared to the same of 1000 most recent images.</p>
  
  <p>Now, I want to make this process faster by saving the most recent avg hashes in a <strong>sorted redis set</strong>, instead of my Postgresql DB. </p>
  
  <p>I'm trying to figure out how to do this. First step would be building a set of avg hashes to compare against, ensuring the set size remains at 1000, and contains the latest 1000 kay-value pairs. </p>
  
  <p>How would the code look like?</p>
  
  <pre><code>import redis
  
  POOL = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0)
  
  def insertValue(photo_hash):
      my_server = redis.Redis(connection_pool=POOL)
      try:
          size = my_server.zcard("my_set")
          if size &lt; 1001:
              my_server.zadd("my_set", int(time.time() * 1000), photo_hash)    #time.time() equals seconds since epoch
          else:
             #zrem the element with the lowest score, and then ...
             my_server.zadd("my_set", int(time.time() * 1000), photo_hash)    
      except:
          my_server.zadd("my_set", int(time.time() * 1000), photo_hash)
  </code></pre>
  
  <p><strong>Firstly</strong>, is the syntax for <code>zadd</code> correct (I can't find online python examples approximating what I'm trying to do), and <strong>secondly</strong>, how does one <code>zrem</code> the element with the lowest score from a sorted set?</p>
  
  <p>Please advise. </p>