Zip to reduce session size for Memcache

I’m running my application on appengine & I have the data of 1MB+ in session, as we know memcache has size limit of 1MB, hence I tried to zip the data (based on a Wicket user’s solution for a similar problem
here
). This seems to be working fine (yet to find any issue…).

However, to do this I had to create a duplicate class of GAEApplicationServlet and do some changes. My suggestion is, similar to Wicket, can there be some ways to allow the dev to have custom impl to zip/unzip or serialize/unserialize the data.

Also, Wicket provides an option to do getFrameworkSettings().setSerializer(new KryoSerializer()); can we have similar option in Vaadin as well?

I understand that this may sound too much to ask… but considering the advantages like zipping reduces the size to 1/3 and Kryo serializer is rated as one of the best & fastest serializer, I believe having these features will add value.

For curious guys, here is what I had tried:

Instead of this @ service method

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(ctx);
oos.flush();
byte[] bytes = baos.toByteArray();

I have

byte[] bytes = Compressor.compress(ctx);

and instead of this @ getApplicationContext method

ois = new ObjectInputStream(bais);
 ApplicationContext applicationContext = (ApplicationContext) ois
                        .readObject();

I have

ApplicationContext applicationContext = (ApplicationContext) Compressor.uncompress(serializedAC);

The Compressor class:

public static Object uncompress(byte[] bytes) {
		if (bytes == null)
			return null;

		try {
			ByteArrayInputStream bais = new ByteArrayInputStream(bytes);

			Inflater def = new Inflater();
			InflaterInputStream dis = new InflaterInputStream(bais, def, 4 * 1024);
			ObjectInputStream objectIn = new ObjectInputStream(dis);
			Object value2 = objectIn.readObject();
			objectIn.close();

			return value2;
		}

		catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static byte[] compress(Object value) {
		ByteArrayOutputStream baos = null;

		try {
			baos = new ByteArrayOutputStream();
			ObjectOutputStream objectOut = new ObjectOutputStream(baos);
			objectOut.writeObject(value);
			objectOut.close();
			System.out.println(baos.toByteArray().length);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

		baos = new ByteArrayOutputStream();
		Deflater def = new Deflater(Deflater.BEST_SPEED);
		DeflaterOutputStream dos = new DeflaterOutputStream(baos, def, 4 * 1024);

		try {
			ObjectOutputStream objectOut = new ObjectOutputStream(dos);

			objectOut.writeObject(value);
			objectOut.close();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		byte[] bytes = baos.toByteArray();

		return bytes;
	}

Caution for new users:
Please be informed that though zipping the context reduces to 1/3 of its size, it may not always happen so. Also there are chances that the zipped data itself is 1MB+ if the data is too large, hence this approach may not be an ideal solution for all cases.

Thanks.