Caching Zip Object With Django Memcached

二次信任 提交于 2020-01-16 17:26:41

问题


I am currently using Django and memcached to keep track of a Zip object like so:

activeList_data = cache.get(active_list_key)

if not activeList_data:
    activeTickers, activeName = get_most_active()
    activeList = zip(activeTickers, activeName)

    cache.set(1, activeList, cache_time)

However, whenever I try to print the cache it just returns a blank list. Any ideas on where I am going wrong? Thanks

EDIT: For reference here is my full code

activeList_data = cache.get(active_list_key) # returns None if no key-value pair

if not activeList_data:
    activeTickers, activeName = get_most_active()
    print(activeTickers)

    if not activeTickers:
        # If blank list returned (weekends) then use 
        # previous data stored in models
        tickerList = []
        nameList = []

        most_active = MostActiveStock.objects.all()

        for ticker in most_active.ticker:
            tickerList.append(ticker)

        for name in most_active.name:
            nameList.append(name)

        activeList = zip(activeTickers, activeName)
        print(1)
    else:
        activeTickers = activeTickers[:3]
        activeName = activeName[:3]

        activeList = zip(activeTickers, activeName)
        print(list(activeList))

        # Delete all old data
        MostActiveStock.objects.all().delete()

        # Store latest data for IEX's weekend blackouts 
        for ticker, name in activeList:
            most_active_obj = MostActiveStock.objects.create(ticker=ticker, name=name)

        print(2)

    cache.set(active_list_key, activeList, cache_time)
    print(3)
else:
    activeList = activeList_data
    print(4)
    print(list(activeList))

context_dict['activeList'] = activeList

来源:https://stackoverflow.com/questions/52711283/caching-zip-object-with-django-memcached

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!