Example #1
0
/** Save the timestamp and offline status for a model */
function saveModelInfo(id, config, offline) {
	// Logger.debug('Saving ' + config.collection_name + '/' + id + ' offline ' + value + '...');
	if (getInfo({ id: id, config: {adapter: { collection_name : config.collection_name }}}) != null) {
		DB.table(config.infoTableName)
		.where({
			m_id: String(id),
			m_table: config.collection_name
		})
		.update({
			offline: ~~offline,
			timestamp: Util.now()
		})
		.run();
	} else {
		DB.table(config.infoTableName)
		.insert({
			m_id: String(id),
			m_table: config.collection_name,
			file_name: config.file_name,
			offline: ~~offline,
			timestamp: Util.now()
		})
		.run();
	}
}
Example #2
0
	function updateInfo(response) {
		// Update the timestamp
		var mId = String(response[model.idAttribute || 'id']);
		var mTable = config.collection_name;

		if (getInfo({ id: mId, config: {adapter: { collection_name : mTable }}}) != null) { // Also works with an object
			DB.table(config.infoTableName)
			.where({
				m_id: mId,
				m_table: mTable
			})
			.update({
				timestamp: Util.now()
			})
			.run();
		} else {
			DB.table(config.infoTableName)
			.insert({
				m_id: mId,
				m_table: mTable,
				file_name: config.file_name,
				offline: 0,
				timestamp: Util.now()
			})
			.run();
		}
	}
Example #3
0
HTTPRequest.prototype._getResponseInfo = function() {
	if (this.client == null || this.client.readyState <= 1) {
		throw new Error('HTTP.Request: Client is null or not ready');
	}

	var headers = {
		Expires: this.client.getResponseHeader('Expires'),
		ContentType: this.client.getResponseHeader('Content-Type'),
		TTL: this.client.getResponseHeader('X-Cache-Ttl')
	};

	var info = {
		format: 'blob',
		ttl: 0
	};

	if (this.client.responseText != null) {
		info.format = 'text';
		if (/^application\/json/.test(headers.ContentType)) info.format = 'json';
	}

	// Always prefer X-Cache-Ttl over Expires
	if (headers.TTL != null) {
		info.ttl = headers.TTL;
	} else if (headers.Expires != null) {
		info.ttl = Util.timestamp(headers.Expires) - Util.now();
	}

	// Override
	if (this.opt.format != null) info.format = this.opt.format;
	if (this.opt.ttl != null) info.ttl = this.opt.ttl;

	return info;
};
Example #4
0
HTTPRequest.prototype.getCachedResponse = function() {
	if (HTTP.config.useCache === false) return;
	if (this.opt.cache === false || this.opt.refresh === true) return;
	if (this.method !== 'GET') return;

	var cachedData = Cache.get(this.hash);
	if (cachedData == null) return;

	Ti.API.debug('HTTP: ['+this.hash+'] CACHE SUCCESS for '+(cachedData.expire-Util.now())+'s');

	return extractHTTPText(cachedData.value, cachedData.info);
};
Example #5
0
exports.get = function(hash) {
	var row = DB.row('SELECT expire, value, info FROM cache WHERE hash = ? LIMIT 1', hash);
	if (_.isEmpty(row)) return null;

	var expire = parseInt(row.expire, 10);
	if (expire !== -1 && Util.now() > expire) return null;

	return {
		expire: expire,
		value: row.value,
		info: Util.parseJSON(row.info)
	};
};
exports.get = function(hash) {
	var row = DB.row('SELECT expire, info FROM ' + TABLE + ' WHERE hash = ? LIMIT 1', hash);
	if (row == null) return null;

	var expire = row.expire << 0;
	if (Util.now() > expire) return null;

	var file = Ti.Filesystem.getFile(DIR, hash);
	if (!file.exists()) return null;

	return {
		value: file.read(),
		expire: expire,
		info: Util.parseJSON(row.info)
	};
};
Example #7
0
/** Add a row in the sync table for the call to a method for a model. */
function addSyncForModel(method, model, opt) {
	var mId = String(model.id);
	var mTable = model.config.adapter.collection_name;

	// Insert the new sync call
	DB.table(exports.config.syncTableName)
	.insert({
		timestamp: Util.now(),
		m_id: mId,
		m_table: mTable,
		method: method,
		file_name: model.config.adapter.file_name,
		model: JSON.stringify(model ? model.toJSON() : {}),
		options: JSON.stringify(opt || {})
	})
	.run();
}
Example #8
0
HTTPRequest.prototype.getCachedResponse = function() {
	if (this.method !== 'GET') return null;

	if (exports.config.useCache === false) {
		Ti.API.trace('HTTP: <' + this.uniqueId + '> cache has been disabled globally');
		return null;
	}

	if (this.opt.cache === false || this.opt.refresh === true) {
		Ti.API.trace('HTTP: <' + this.uniqueId + '> get cache has been disabled for this request');
		return null;
	}

	this.cachedData = Cache.get(this.hash);
	if (this.cachedData == null) {
		Ti.API.trace('HTTP: <' + this.uniqueId + '> cache is missing');
		return null;
	}

	// We got cache

	Ti.API.trace('HTTP: <' + this.uniqueId + '> cache hit up to ' + (this.cachedData.expire - Util.now()) + 's');

	if (this.cachedData.info.format === 'blob') {
		return this.cachedData.value;
	}

	return extractHTTPText(this.cachedData.value.text, this.cachedData.info);
};
Example #9
0
SQLREST.prototype._isLocalValid = function() {
	var self = this;
	var info_row = getInfo(self.model);

	return info_row != null && ((Boolean(info_row.offline) && info_row.timestamp != null) || info_row.timestamp + self.config.ttl > Util.now());
};
Example #10
0
exports.setFirstUse = function(prefix) {
	prefix = prefix || '';
	Ti.App.Properties.setString('app.firstuse' + prefix, Util.now());
};
 */
exports.purge = function() {
	DB.run('DELETE FROM ' + TABLE + ' WHERE 1');
	Ti.Filesystem.getFile(DIR).deleteDirectory(true);
	Ti.Filesystem.getFile(DIR).createDirectory();
};

/**
 * @return {Number}
 */
exports.getSize = function() {
	return require('T/filesystem').getSize(DIR);
};


/*
Init
*/

Ti.Filesystem.getFile(Ti.Filesystem.applicationCacheDirectory).createDirectory();
Ti.Filesystem.getFile(DIR).createDirectory();

var DB = new SQLite('app');
DB.run('CREATE TABLE IF NOT EXISTS ' + TABLE + ' (hash TEXT PRIMARY KEY, expire INTEGER, info TEXT)');

// Delete oldest keys
DB.list('SELECT hash FROM ' + TABLE + ' WHERE expire < ' + Util.now()).forEach(function(h) {
	Ti.API.trace('Cache: removing expired key ' + h);
	exports.remove(h);
});