Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
LOGGER.info('Loading cache 2')
with lock_uwsgi_cache2:
load_uwsgi_cache('cache_chunks2', 'main_cache2', 'cache_modules2', on_change)
LOGGER.info('Both caches are loaded')
else:
# if we need to get some data from api
if active_cache[1] == '1':
# From cache 1
with lock_uwsgi_cache1:
load_uwsgi_cache('cache_chunks1', 'main_cache1', 'cache_modules1', on_change)
# reset active cache back to 1 since we are done with populating cache 1
uwsgi.cache_update('active_cache', '1', 0, 'cache_chunks1')
LOGGER.info('Using cache 1')
else:
with lock_uwsgi_cache2:
initialized = uwsgi.cache_get('initialized', 'cache_chunks2')
LOGGER.debug('initialized {} on change {}'.format(initialized, on_change))
if initialized is not None and initialized == 'True':
load_uwsgi_cache('cache_chunks2', 'main_cache2', 'cache_modules2', on_change)
LOGGER.info('Using cache 2')
def test_lru(self):
self.assertTrue(uwsgi.cache_set('KEY1', 'X' * 20, 0, 'items_lru'))
self.assertTrue(uwsgi.cache_set('KEY2', 'X' * 20, 0, 'items_lru'))
self.assertTrue(uwsgi.cache_set('KEY3', 'Y' * 20, 0, 'items_lru'))
self.assertIsNone(uwsgi.cache_get('KEY1', 'items_lru'))
uwsgi.cache_get('KEY3', 'items_lru')
for i in range(4, 100):
self.assertTrue(uwsgi.cache_set('KEY%d' % i, 'Y' * 20, 0, 'items_lru'))
self.assertIsNone(uwsgi.cache_get('KEY%d' % (i-2), 'items_lru'))
def catalog_data(which_cache):
chunks = int(uwsgi.cache_get('chunks-data', 'cache_chunks{}'.format(which_cache)))
if chunks == 0:
return None
data = ''
for i in range(0, chunks, 1):
data += uwsgi.cache_get('data{}'.format(i), 'main_cache{}'.format(which_cache))
json_data = \
json.JSONDecoder(object_pairs_hook=collections.OrderedDict) \
.decode(data)
return json_data
def load_uwsgi_cache(cache_chunks, main_cache, cache_modules, on_change):
response = 'work'
initialized = uwsgi.cache_get('initialized', cache_chunks)
LOGGER.debug('initialized {} on change {}'.format(initialized, on_change))
if initialized is None or initialized == 'False' or on_change:
uwsgi.cache_clear(cache_chunks)
uwsgi.cache_clear(main_cache)
uwsgi.cache_clear(cache_modules)
if cache_chunks == 'cache_chunks1':
# set active cache to 2 until we work on cache 1
uwsgi.cache_set('active_cache', '2', 0, 'cache_chunks1')
uwsgi.cache_set('initialized', 'False', 0, cache_chunks)
response, data = make_cache(application.credentials, response, cache_chunks, main_cache, is_uwsgi=application.is_uwsgi)
cat = \
json.JSONDecoder(object_pairs_hook=collections.OrderedDict) \
.decode(data)['yang-catalog:catalog']
modules = cat['modules']
if cat.get('vendors'):
def test_lru(self):
self.assertTrue(uwsgi.cache_set('KEY1', 'X' * 20, 0, 'items_lru'))
self.assertTrue(uwsgi.cache_set('KEY2', 'X' * 20, 0, 'items_lru'))
self.assertTrue(uwsgi.cache_set('KEY3', 'Y' * 20, 0, 'items_lru'))
self.assertIsNone(uwsgi.cache_get('KEY1', 'items_lru'))
uwsgi.cache_get('KEY3', 'items_lru')
for i in range(4, 100):
self.assertTrue(uwsgi.cache_set('KEY%d' % i, 'Y' * 20, 0, 'items_lru'))
self.assertIsNone(uwsgi.cache_get('KEY%d' % (i-2), 'items_lru'))
def application(env, start_response):
print env['wsgi.input'].read()
if uwsgi.loop == 'gevent':
gevent.sleep()
start_response('200 OK', [('Content-Type', 'text/html')])
yield "foobar<br>"
if uwsgi.loop == 'gevent':
gevent.sleep(3)
yield str(env['wsgi.input'].fileno())
yield "<h1>Hello World</h1>"
try:
yield uwsgi.cache_get('foo')
except:
pass
def search_module(name, revision, organization):
"""Search for a specific module defined with name, revision and organization
Arguments:
:param name: (str) name of the module
:param revision: (str) revision of the module
:param organization: (str) organization of the module
:return response to the request with job_id that user can use to
see if the job is still on or Failed or Finished successfully
"""
active_cache = get_active_cache()
with active_cache[0]:
LOGGER.info('Searching for module {}, {}, {}'.format(name, revision,
organization))
if uwsgi.cache_exists(name + '@' + revision + '/' + organization,
'cache_chunks{}'.format(active_cache[1])):
chunks = uwsgi.cache_get(name + '@' + revision + '/' + organization,
'cache_chunks{}'.format(active_cache[1]))
data = ''
for i in range(0, int(chunks), 1):
data += uwsgi.cache_get(name + '@' + revision + '/' +
organization + '-' + repr(i),
'cache_modules{}'.format(active_cache[1]))
return Response(json.dumps({
'module': [json.JSONDecoder(object_pairs_hook=collections.OrderedDict)\
.decode(data)]
}), mimetype='application/json')
return not_found()
def __getitem__(self, item):
return uwsgi.cache_get(item)