Memcached Compare-And-Set pattern yields wrong result - django

I'm trying to implement memcached compare-and-set pattern, following the instructions of Guido at:
http://neopythonic.blogspot.nl/2011/08/compare-and-set-in-memcache.html
However, I don't seem to get it right and I have no idea what's wrong. The files below use Django (1.4.5 Final) and python-memcache (1.48).
settings.py
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
djangocache.py
#!/usr/bin/env python
from django.core.cache import cache
import multiprocessing.dummy
django_key = "TEST"
cached_key = cache.make_key(django_key).encode("UTF-8")
def add_to_cache(item):
client = cache._cache
#client = cache._lib.Client(cache._servers)
while True:
items = client.gets(cached_key)
if client.cas(cached_key, items+(item,)):
break
if __name__ == "__main__":
cache.set(django_key, ())
p = multiprocessing.dummy.Pool(2)
p.map(add_to_cache, range(10))
print(len(cache.get(django_key)))
Running it:
mzialla#Q330 ~/test $ DJANGO_SETTINGS_MODULE=settings python djangocache.py
5
It occasionally outputs 6, 7, etc. like you would expect when dealing with race conditions. I've tried multiple client instantiations (see comment).
Help?

python-memcached disables cas by default. Enable it by adding
client.cache_cas = True
to your code.
Credits to Nate Thelen, who's comment I discovered right after asking this question.

Related

Problem with Running okta-jwt-verifier in docker container

I am writing a flask-api where token-verification is done via okta-jwt-verifier package.
I have this code to verify tokens:
import asyncio
from okta_jwt_verifier import AccessTokenVerifier, JWTVerifier
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
def is_access_token_valid(token, issuer,client_id):
jwt_verifier = JWTVerifier(issuer=issuer, client_id=client_id,
audience='api://default', leeway=60)
try:
verified_token = jwt_verifier.verify_access_token(token)
parsed=jwt_verifier.parse_token(token)
g.decoded_token=parsed
loop.run_until_complete(verified_token)
return True
except Exception:
print("Exception")
return False
It works great when i run this on my machine, but when I do this inside docker-container (still on my machine) (i have docker-compose.yml file with 2 services: flask and db(PostgreSQL)), the process fails at loop.run_until_complete(verified_token). I am not sure how to work around that issue. Please help if you have any ideas! Thanks in advance!

django-cms==3.2.3 migration from Django 1.5.12 to 1.9.5 choices error

While working a migration of an older Django project I ran into this error after running:
python manage.py check
cms.UserSettings.language: (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples.
Has anyone run into this issue? Unfortunately I have to wait until I am not on the corp network before I can ask the IRC channels.
http://docs.django-cms.org/en/latest/reference/configuration.html#cms-languages
It turns out I missed this important setting in my settings.py file:
CMS_LANGUAGES = {
'default': {
'fallbacks': ['en',],
'redirect_on_fallback':True,
'public': True,
'hide_untranslated': False,
}
}
Thanks to brianpck for a point in the right direction though.

Why timezone inside unittest in Django is different from timezone in view?

I'm using TestCase.client to test my views. And when I call timezone.now() from test case, i get 2015-11-17 07:48:26.826661+00:00, but when I call
start = timezone.make_aware(datetime.strptime(
date_text + ' ' + time,
'%y/%m/%d %H:%M'
))
from view I get 2015-11-17 07:36:00+02:00.
How to make them use the same timezone?
I'm running using ./manage.py test --settings=www.tests_settings and www/tests_settings.py contains following:
from .settings import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
}
}
USE_TZ = True
TIME_ZONE = 'Europe/Kiev'
>>> django.__version__
'1.8.4'
>>> pytz.__version__
'2015.7'
>>> sys.version
'3.4.3 (default, Mar 26 2015, 22:03:40) \n[GCC 4.9.2]'
Oh, I see. make_aware returns time in default time zone, and now - in UTC. And to get local current time, I need to do timezone.localtime(timezone.now()).
But documentation recommends to deal with time and timezones like with text and encodings - store everything in UTC (unicode) and convert only when receiving or giving it to user.

Using Memcache in Django/Python 3,4 with Heroku

I have been following this tutorial so that I can use Memcache on my app on Heroku. However I ran into issues when using cache.get() in Heroku's shell (it works fine on my end):
File "/app/.heroku/python/lib/python3.4/site-packages/django_pylibmc/memcached.py", line 92
except MemcachedError, e:
^
SyntaxError: invalid syntax
I saw this question, who had the same issue as me. My settings.py looked like this:
def get_cache():
import os
try:
os.environ['MEMCACHE_SERVERS'] = os.environ['MEMCACHIER_SERVERS'].replace(',', ';')
os.environ['MEMCACHE_USERNAME'] = os.environ['MEMCACHIER_USERNAME']
os.environ['MEMCACHE_PASSWORD'] = os.environ['MEMCACHIER_PASSWORD']
return {
'default': {
'BACKEND': 'django_pylibmc.memcached.PyLibMCCache',
'TIMEOUT': 500,
'BINARY': True,
'OPTIONS': { 'tcp_nodelay': True }
}
}
except:
return {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'
}
}
And so I replaced django_pylibmc.memcached.PyLibMCCache with django.core.cache.backends.memcached.PyLibMCCache. However then I got a different error when I tried cache.get("foo") again:
File "/app/.heroku/python/lib/python3.4/site-packages/django/core/cache/backends/memcached.py", line 84, in get
val = self._cache.get(key)
_pylibmc.ConnectionError: error 3 from memcached_get(:1:foo): (0x1c4ed40) CONNECTION FAILURE, host: localhost:11211 -> libmemcached/get.cc:314
Could someone help me out? Is there some settings somewhere that I need to change?
The syntax
except MemcachedError, e:
works in Python 2 but not Python 3.
Change it to except MemcachedError as e: and you should be fine.
I decided to switch to using Django's database cache instead.

Django settings.DATABASE 'default' value is not set

I have a Django 1.4.3 project using Python 2.7.3 on Ubuntu 12.10. In my entire project, there is only one settings.py file, and in manage.py, I have this line
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")
Of course, the name of my project is hellodjango, and the settings.py file is in the subfolder hellodjango (they have the same name).
When I type the following line into the console:
python manage.py diffsettings
These are my results for DATABASES:
DATABASES = {'default': {}, 'mioa': {'1': 'this thingy', '2': 'that thingy'}}
The relevant lines in settings.py are these:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'mydatabase'
},
'mioa': {
'1': 'this thingy',
'2': 'that thingy'
}
}
I have the 'mioa' entry in there to demonstrate my problem. For some reason, the 'default' field is not getting set. Does anyone know why this is happening? Or what the solution is?
Help on diffsettings says:
Displays differences between the current settings.py and Django's
default settings
which means, that it show only difference between your value and default one. Seems like your value is completely the same as the default one, so you don't see any difference.
Try another engine and/or database name and you'll see it's changed.