日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android sharedPreference设置缓存时间

發布時間:2024/4/15 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android sharedPreference设置缓存时间 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

不廢話,需求:緩存登錄時的token,超過設置的存儲時間就無效,怎么做?

使用ACache也可以設置緩存時間,但ACache在清緩存的時候會被清空。?
SharedPreferences存儲默認都是無時間限制的。?
大概思路是,存儲的時候記錄當前時間,要存多久。取數據的時候判斷這個數據已經存儲了多久,如果超過設置的存儲時間,就獲取默認值。?
首先,我們需要一個存儲的model——SpSaveModel
?

public class SpSaveModel<T> implements Serializable{private int saveTime;private T value;private long currentTime;public SpSaveModel() {}public SpSaveModel(int saveTime, T value,long currentTime) {this.saveTime = saveTime;this.value = value;this.currentTime=currentTime;}public long getCurrentTime() {return currentTime;}public void setCurrentTime(long currentTime) {this.currentTime = currentTime;}public int getSaveTime() {return saveTime;}public void setSaveTime(int saveTime) {this.saveTime = saveTime;}public T getValue() {return value;}public void setValue(T value) {this.value = value;} }

需要一個object和json字符串轉換的工具,這里用fastJson,添加依賴

compile 'com.alibaba:fastjson:1.1.26' import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.text.TextUtils;import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference;/*** Created by KID on 2018/5/3.*/ public class SpUtils {//保存時間單位public static final int TIME_SECOND=1;public static final int TIME_MINUTES=60*TIME_SECOND;public static final int TIME_HOUR = 60 *TIME_MINUTES;public static final int TIME_DAY = TIME_HOUR * 24;public static final int TIME_MAX = Integer.MAX_VALUE; // 不限制存放數據的數量public static final int DURATION_UNIT=1000;private static final String fileName = "config";private SharedPreferences sp;private Editor editor;private static SpUtils INSTANCE = null;public static SpUtils getInstance(Context context) {if (null == INSTANCE) {INSTANCE = new SpUtils(context);}return INSTANCE;}private SpUtils(Context context) {sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);editor = sp.edit();}public void setString(String e, String value) {SpSaveModel<String>spSaveModel=new SpSaveModel<>(TIME_MAX,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}/**** @param e 存放的key* @param value 存放的value* @param saveTime 緩存時間*/public void setString(String e, String value,int saveTime) {SpSaveModel<String>spSaveModel=new SpSaveModel<>(saveTime,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}/**** @param e 存放的key* @param defValue 該key不存在或者過期時,返回的默認值* @return*/public String getString(String e, String defValue){String json=sp.getString(e,"");if(!TextUtils.isEmpty(json)){SpSaveModel<String>spSaveModel= JSON.parseObject(json, new TypeReference<SpSaveModel<String>>(){});if(isTimeOut(spSaveModel.getCurrentTime(),spSaveModel.getSaveTime())){return spSaveModel.getValue();}}return defValue;}public void setInt(String e, int value) {SpSaveModel<Integer>spSaveModel=new SpSaveModel<>(TIME_MAX,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}public void setInt(String e, int value,int saveTime) {SpSaveModel<Integer>spSaveModel=new SpSaveModel<>(saveTime,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}public Integer getInt(String e, int defValue){String json=sp.getString(e,"");if(!TextUtils.isEmpty(json)){SpSaveModel<Integer>spSaveModel= JSON.parseObject(json, new TypeReference<SpSaveModel<Integer>>(){});if(isTimeOut(spSaveModel.getCurrentTime(),spSaveModel.getSaveTime())){return spSaveModel.getValue();}}return defValue;}public void setBoolean(String e, boolean value) {SpSaveModel<Boolean>spSaveModel=new SpSaveModel<>(TIME_MAX,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}public void setBoolean(String e, boolean value,int saveTime) {SpSaveModel<Boolean>spSaveModel=new SpSaveModel<>(saveTime,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}public boolean getBoolean(String e, boolean defValue){String json=sp.getString(e,"");if(!TextUtils.isEmpty(json)){SpSaveModel<Boolean>spSaveModel= JSON.parseObject(json, new TypeReference<SpSaveModel<Boolean>>(){});if(isTimeOut(spSaveModel.getCurrentTime(),spSaveModel.getSaveTime())){return spSaveModel.getValue();}}return defValue;}public void setLong(String e, long value) {SpSaveModel<Long>spSaveModel=new SpSaveModel<>(TIME_MAX,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}public void setLong(String e, long value,int saveTime) {SpSaveModel<Long>spSaveModel=new SpSaveModel<>(saveTime,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}public long getLong(String e, long defValue){String json=sp.getString(e,"");if(!TextUtils.isEmpty(json)){SpSaveModel<Long>spSaveModel= JSON.parseObject(json, new TypeReference<SpSaveModel<Long>>(){});if(isTimeOut(spSaveModel.getCurrentTime(),spSaveModel.getSaveTime())){return spSaveModel.getValue();}}return defValue;}public boolean isTimeOut(long saveCurrentTime,int saveTime){return (System.currentTimeMillis()-saveCurrentTime)/DURATION_UNIT<saveTime;}public void set(String e, Object value,int saveTime) {SpSaveModel<Object>spSaveModel=new SpSaveModel<>(saveTime,value,System.currentTimeMillis());String json=JSON.toJSONString(spSaveModel);editor.putString(e, json);editor.commit();}}

使用

public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.btn_save).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {save();}});findViewById(R.id.btn_get).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {get();}});}//保存private void save() {//保存10秒 // SpUtils.getInstance(this).set("text","我的世界",10);SpUtils.getInstance(this).setString("text","我的世界",10);SpUtils.getInstance(this).setInt("num",123456,10);SpUtils.getInstance(this).setBoolean("isBu",true,10);}//獲取private void get() {String text=SpUtils.getInstance(this).getString("text","超時了");int num=SpUtils.getInstance(this).getInt("num",-100);boolean isBu=SpUtils.getInstance(this).getBoolean("isBu",false);Log.e("kid","text======="+text);Log.e("kid","num======="+num);Log.e("kid","isBu======="+isBu);}}

PS:存的時候,不加存儲時間,默認是永久
參考文章:https://blog.csdn.net/qq_31390699/article/details/80182661? 寫得很好,我直接用的

?

?

?

?

總結

以上是生活随笔為你收集整理的Android sharedPreference设置缓存时间的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。