H5游戏开发之IndexedDB实现

IndexedDB使用

使用IndexedDB来缓存游戏图片

针对H5游戏开发资源加载慢的问题,保存图片资源到本地缓存,加快资源加载速度。好的,闲话少说,切入正题,下面讲具体的操作步骤。

IndexedDB

module game {
	/**资源数据库处理 */
	export class ResDBMgr {
		private static _indexedDb;
		public static db;
		public static hasInit = false;
		public static hasDB = false;
		
		public static dbVersion = 108;
		public static dbName = "resource";
		
		public constructor() {
		}	
		/**
		 * 获取图片资源
		 * url  : 链接Url
		 * callFunc  成功回调
		 * errFunc   失败回调		 
		 *  */
		public static getImage(url , callFunc , errFunc , callObj)
		{	
			try{
				let tran =  this.db.transaction(this.dbName, "readwrite");
				let store = tran.objectStore(this.dbName);
				if(url)
				{
					let request = store.get(url);
					if(!request) 
					{
						if(errFunc)
						{
							errFunc.call(callObj  , url);
						}
						return;
					} 
					request.onsuccess = function(){
						let data = request.result;
						if(data && data.res)
						{
							if(callFunc)
							{
								callFunc.call(callObj ,  data['res'] , url);
							}
						}else{
							if(errFunc)
							{
								errFunc.call(callObj  , url);
							}
						}
					}
					request.onerror = function(){
						if(errFunc)
						{
							errFunc.call(callObj  , url);
						}
					}
				}
			}catch(e){
				//报错处理
				if(errFunc)
				{
					errFunc.call(callObj  , url);
				}
			}
		}
		/**保存数据操作 */
		public static saveImage(url , res): void {	
			if(this.db)		
			{
				var transaction = this.db.transaction(this.dbName, "readwrite");
				var put = transaction.objectStore(this.dbName).put({url:url , res:res});			
				put.onerror = function(evt){
					ResDBMgr.deleteImg(url);
				}
			}
		}
		/**删除资源处理 */
		public static deleteImg(url:string):void
		{
			let request = this.db.transaction(this.dbName , "readwrite").objectStore(this.dbName).delete(url);		
		}
		public static clear():void
		{
			var request = this.db.transaction(this.dbName,"readwrite").objectStore(this.dbName).clear();			
		}

		public static init(): void {
			// IndexedDB
			if(this.hasInit)
			{
				return;
			}
			this.hasInit = true;
			var indexedDB = window.indexedDB || window['webkitIndexedDB'] || window['mozIndexedDB'] || window['OIndexedDB'] || window['msIndexedDB'],
				IDBTransaction = window['IDBTransaction'] || window['webkitIDBTransaction'] || window['OIDBTransaction'] || window['msIDBTransaction'],
				dbVersion = this.dbVersion;
			this._indexedDb = indexedDB;			
			let that = this;
			//创建数据库处理
			var request = indexedDB.open("jdqk_resource", dbVersion);
			var db;
			request.onerror = function (event) {
				//数据库错误处理
			};
			request.onsuccess = function (evt) {
				db = evt.target.result;
				that.db = db;				
			}
			request.onupgradeneeded = function (evt) {
				db = evt.target.result;			
				if(!db.objectStoreNames.contains(that.dbName))				
				{
					db.createObjectStore(that.dbName , {keyPath:"url"}); 
				}
				that.db = db;
			};
		}
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值