示例#1
0
 def testRegex(self):
     cache = CacheInRam()
     self.assertEqual(cache('a1', lambda: 1, 0), 1)
     self.assertEqual(cache('a2', lambda: 2, 100), 2)
     cache.clear(regex=r'a*')
     self.assertEqual(cache('a1', lambda: 2, 0), 2)
     self.assertEqual(cache('a2', lambda: 3, 100), 3)
示例#2
0
 def test_Regex(self):
     cache = CacheInRam()
     self.assertEqual(cache('a1', lambda: 1, 0), 1)
     self.assertEqual(cache('a2', lambda: 2, 100), 2)
     cache.clear(regex=r'a*')
     self.assertEqual(cache('a1', lambda: 2, 0), 2)
     self.assertEqual(cache('a2', lambda: 3, 100), 3)
示例#3
0
def copystream_progress(request, chunk_size=10 ** 5):
    """
    copies request.env.wsgi_input into request.body
    and stores progress upload status in cache_ram
    X-Progress-ID:length and X-Progress-ID:uploaded
    """
    env = request.env
    if not env.get('CONTENT_LENGTH', None):
        return cStringIO.StringIO()
    source = env['wsgi.input']
    try:
        size = int(env['CONTENT_LENGTH'])
    except ValueError:
        raise HTTP(400, "Invalid Content-Length header")
    try: # Android requires this
        dest = tempfile.NamedTemporaryFile()
    except NotImplementedError: # and GAE this
        dest = tempfile.TemporaryFile()
    if not 'X-Progress-ID' in request.get_vars:
        copystream(source, dest, size, chunk_size)
        return dest
    cache_key = 'X-Progress-ID:' + request.get_vars['X-Progress-ID']
    cache_ram = CacheInRam(request)  # same as cache.ram because meta_storage
    cache_ram(cache_key + ':length', lambda: size, 0)
    cache_ram(cache_key + ':uploaded', lambda: 0, 0)
    while size > 0:
        if size < chunk_size:
            data = source.read(size)
            cache_ram.increment(cache_key + ':uploaded', size)
        else:
            data = source.read(chunk_size)
            cache_ram.increment(cache_key + ':uploaded', chunk_size)
        length = len(data)
        if length > size:
            (data, length) = (data[:size], size)
        size -= length
        if length == 0:
            break
        dest.write(data)
        if length < chunk_size:
            break
    dest.seek(0)
    cache_ram(cache_key + ':length', None)
    cache_ram(cache_key + ':uploaded', None)
    return dest
示例#4
0
def copystream_progress(request, chunk_size=10 ** 5):
    """
    copies request.env.wsgi_input into request.body
    and stores progress upload status in cache_ram
    X-Progress-ID:length and X-Progress-ID:uploaded
    """
    env = request.env
    if not env.content_length:
        return cStringIO.StringIO()
    source = env.wsgi_input
    try:
        size = int(env.content_length)
    except ValueError:
        raise HTTP(400, "Invalid Content-Length header")
    dest = tempfile.TemporaryFile()
    if not "X-Progress-ID" in request.vars:
        copystream(source, dest, size, chunk_size)
        return dest
    cache_key = "X-Progress-ID:" + request.vars["X-Progress-ID"]
    cache_ram = CacheInRam(request)  # same as cache.ram because meta_storage
    cache_ram(cache_key + ":length", lambda: size, 0)
    cache_ram(cache_key + ":uploaded", lambda: 0, 0)
    while size > 0:
        if size < chunk_size:
            data = source.read(size)
            cache_ram.increment(cache_key + ":uploaded", size)
        else:
            data = source.read(chunk_size)
            cache_ram.increment(cache_key + ":uploaded", chunk_size)
        length = len(data)
        if length > size:
            (data, length) = (data[:size], size)
        size -= length
        if length == 0:
            break
        dest.write(data)
        if length < chunk_size:
            break
    dest.seek(0)
    cache_ram(cache_key + ":length", None)
    cache_ram(cache_key + ":uploaded", None)
    return dest
示例#5
0
    def testCacheInRam(self):

        # defaults to mode='http'

        cache = CacheInRam()
        self.assertEqual(cache('a', lambda: 1, 0), 1)
        self.assertEqual(cache('a', lambda: 2, 100), 1)
        cache.clear('b')
        self.assertEqual(cache('a', lambda: 2, 100), 1)
        cache.clear('a')
        self.assertEqual(cache('a', lambda: 2, 100), 2)
        cache.clear()
        self.assertEqual(cache('a', lambda: 3, 100), 3)
        self.assertEqual(cache('a', lambda: 4, 0), 4)
示例#6
0
 def testRun(self):
     from cache import CacheInRam
     cache = CacheInRam()
     db = DAL(DEFAULT_URI, check_reserved=['all'])
     db.define_table('tt', Field('aa'))
     db.tt.insert(aa='1')
     r0 = db().select(db.tt.ALL)
     r1 = db().select(db.tt.ALL, cache=(cache, 1000))
     self.assertEqual(len(r0), len(r1))
     r2 = db().select(db.tt.ALL, cache=(cache, 1000))
     self.assertEqual(len(r0), len(r2))
     r3 = db().select(db.tt.ALL, cache=(cache, 1000), cacheable=True)
     self.assertEqual(len(r0), len(r3))
     r4 = db().select(db.tt.ALL, cache=(cache, 1000), cacheable=True)
     self.assertEqual(len(r0), len(r4))
     db.tt.drop()
示例#7
0
 def testRun(self):
     from cache import CacheInRam
     cache = CacheInRam()
     db = DAL('sqlite:memory:')
     db.define_table('t', Field('a'))
     db.t.insert(a='1')
     r0 = db().select(db.t.ALL)
     r1 = db().select(db.t.ALL, cache=(cache, 1000))
     self.assertEqual(len(r0), len(r1))
     r2 = db().select(db.t.ALL, cache=(cache, 1000))
     self.assertEqual(len(r0), len(r2))
     r3 = db().select(db.t.ALL, cache=(cache, 1000), cacheable=True)
     self.assertEqual(len(r0), len(r3))
     r4 = db().select(db.t.ALL, cache=(cache, 1000), cacheable=True)
     self.assertEqual(len(r0), len(r4))
     db.t.drop()
    def testCacheInRam(self):

        # defaults to mode='http'

        cache = CacheInRam()
        self.assertEqual(cache('a', lambda: 1, 0), 1)
        self.assertEqual(cache('a', lambda: 2, 100), 1)
        cache.clear('b')
        self.assertEqual(cache('a', lambda: 2, 100), 1)
        cache.clear('a')
        self.assertEqual(cache('a', lambda: 2, 100), 2)
        cache.clear()
        self.assertEqual(cache('a', lambda: 3, 100), 3)
        self.assertEqual(cache('a', lambda: 4, 0), 4)
示例#9
0
def copystream_progress(request, chunk_size=10**5):
    """
    copies request.env.wsgi_input into request.body
    and stores progress upload status in cache_ram
    X-Progress-ID:length and X-Progress-ID:uploaded
    """
    env = request.env
    if not env.get('CONTENT_LENGTH', None):
        return cStringIO.StringIO()
    source = env['wsgi.input']
    try:
        size = int(env['CONTENT_LENGTH'])
    except ValueError:
        raise HTTP(400, "Invalid Content-Length header")
    try:  # Android requires this
        dest = tempfile.NamedTemporaryFile()
    except NotImplementedError:  # and GAE this
        dest = tempfile.TemporaryFile()
    if not 'X-Progress-ID' in request.get_vars:
        copystream(source, dest, size, chunk_size)
        return dest
    cache_key = 'X-Progress-ID:' + request.get_vars['X-Progress-ID']
    cache_ram = CacheInRam(request)  # same as cache.ram because meta_storage
    cache_ram(cache_key + ':length', lambda: size, 0)
    cache_ram(cache_key + ':uploaded', lambda: 0, 0)
    while size > 0:
        if size < chunk_size:
            data = source.read(size)
            cache_ram.increment(cache_key + ':uploaded', size)
        else:
            data = source.read(chunk_size)
            cache_ram.increment(cache_key + ':uploaded', chunk_size)
        length = len(data)
        if length > size:
            (data, length) = (data[:size], size)
        size -= length
        if length == 0:
            break
        dest.write(data)
        if length < chunk_size:
            break
    dest.seek(0)
    cache_ram(cache_key + ':length', None)
    cache_ram(cache_key + ':uploaded', None)
    return dest
示例#10
0
    def testCacheInRam(self):

        # defaults to mode='http'
        cache = CacheInRam()
        self.assertEqual(cache('a', lambda: 1, 0), 1)
        self.assertEqual(cache('a', lambda: 2, 100), 1)
        cache.clear('b')
        self.assertEqual(cache('a', lambda: 2, 100), 1)
        cache.clear('a')
        self.assertEqual(cache('a', lambda: 2, 100), 2)
        cache.clear()
        self.assertEqual(cache('a', lambda: 3, 100), 3)
        self.assertEqual(cache('a', lambda: 4, 0), 4)
        #test singleton behaviour
        cache = CacheInRam()
        cache.clear()
        self.assertEqual(cache('a', lambda: 3, 100), 3)
        self.assertEqual(cache('a', lambda: 4, 0), 4)
        #test key deletion
        cache('a', None)
        self.assertEqual(cache('a', lambda: 5, 100), 5)
        #test increment
        self.assertEqual(cache.increment('a'), 6)
        self.assertEqual(cache('a', lambda: 1, 100), 6)
        cache.increment('b')
        self.assertEqual(cache('b', lambda: 'x', 100), 1)
示例#11
0
    def testCacheInRam(self):

        # defaults to mode='http'
        cache = CacheInRam()
        self.assertEqual(cache('a', lambda: 1, 0), 1)
        self.assertEqual(cache('a', lambda: 2, 100), 1)
        cache.clear('b')
        self.assertEqual(cache('a', lambda: 2, 100), 1)
        cache.clear('a')
        self.assertEqual(cache('a', lambda: 2, 100), 2)
        cache.clear()
        self.assertEqual(cache('a', lambda: 3, 100), 3)
        self.assertEqual(cache('a', lambda: 4, 0), 4)
        #test singleton behaviour
        cache = CacheInRam()
        cache.clear()
        self.assertEqual(cache('a', lambda: 3, 100), 3)
        self.assertEqual(cache('a', lambda: 4, 0), 4)
        #test key deletion
        cache('a', None)
        self.assertEqual(cache('a', lambda: 5, 100), 5)
        #test increment
        self.assertEqual(cache.increment('a'), 6)
        self.assertEqual(cache('a', lambda: 1, 100), 6)
        cache.increment('b')
        self.assertEqual(cache('b', lambda: 'x', 100), 1)