数据访问对象模式

本文共--字 阅读约--分钟 | 浏览: -- Last Updated: 2021-12-21

数据访问对象模式(Data access object):抽象和封装对数据源的访问与存储,DAO通过对数据源链接的管理,方便对数据的访问与存储。

/**
 *
 * @param {string} preId key的前缀
 * @param {string} timeSign 时间戳与存储数据之间的拼接符
 */
var BaseLocalStorage = function(preId, timeSign) {
  this.preId = preId;
  this.timeSign = timeSign || '-';
}

BaseLocalStorage.prototype = {
  status: {
    SUCCESS: 0, // 成功
    FAILURE: 1, // 失败
    OVERFLOW: 2, // 溢出
    TIMEOUT: 3, // 过期
  },
  storage: window.localStorage,
  getKey: function(key) {
    // 获取真实存储的key值
    return this.preId + key;
  },
  /**
   *
   * @param {*} key 用户存储key值
   * @param {*} value 用户存储value
   * @param {*} callback 存储操作回调
   * @param {*} time 过期时间
   */
  set: function(key, value, callback, time) {
    var status = this.status.SUCCESS;
    var saveKey = this.getKey(key);
    try {
      time = new Date(time).getTime() || time.getTime();
    } catch (error) {
      time = new Date().getTime() + 1000 * 60 * 60 * 24 * 31; // 默认过期时间为1个月
    }

    try {
      this.storage.setItem(saveKey, time + this.timeSign + value);
    } catch (error) {
      status = this.status.OVERFLOW;
    }

    callback && callback.call(this, status, saveKey, value);
  },
  get: function(key, callback) {
    var status = this.status.SUCCESS;
    var saveKey = this.getKey(key);
    var value = null;
    var timeSignLen = this.timeSign.length;
    var that = this;
    var index;
    var time;
    var result;

    try {
      value = that.storage.getItem(saveKey);
    } catch (error) {
      // 默认失败返回
      result = {
        status: this.status.FAILURE,
        value: null,
      };
      callback && callback.call(this, result.status, result.value)
    }

    if (value) {
      index = value.indexOf(that.timeSign);
      time = +value.slice(0, index);
      // 未过期
      if (new Date(time).getTime() > new Date().getTime() || time == 0) {
        value = value.slice(index + timeSignLen);
      } else {
        value = null;
        status = that.status.TIMEOUT;
        that.remove(key);
      }
    } else {
      status = that.status.FAILURE;
    }

    result = {
      status,
      value
    }

    callback && callback.call(this, result.status, result.value);

    return result;
  },
  remove: function(key, callback) {
    var status = this.status.FAILURE;
    var saveKey = this.getKey(key);
    var value = null;

    try {
      value = this.storage.getItem(saveKey);
    } catch (error) {
      // ignore this
    }

    if (value) {
      try {
        this.storage.removeItem(saveKey);
        status = this.status.SUCCESS;
      } catch (error) {
        // ignore this
      }
    }

    callback && callback.call(this, status, status > 0 ? null : value.slice(value.indexOf(this.timeSign) + this.timeSign.length));
  },
}

var s = new BaseLocalStorage('TESTCZ_');

function print() {
  console.log(arguments);
}

s.set('key1', 'xiao ming', print) // [0, 'TESTCZ_key1', 'xiao ming'] 

s.get('key1', print) // [0, 'xiao ming ']

s.remove('key1', print) // [0, 'xiao ming']

s.remove('key1', print) // [1, null]

s.get('key1', print); // [1, null]

数据访问对象即是对数据路的操作,比如增删查改这些操作进行封装,用户不必为操作数据库感到烦恼,提供统一的操作接口,对于使用者来说,不必了解内部操作是如何实现的,也不必了解数据是如何操作的,方便使用。