mirror of
https://github.com/ZSCNetSupportDept/WechatTicketSystem.git
synced 2025-10-29 08:05:04 +08:00
115 lines
3.8 KiB
Java
115 lines
3.8 KiB
Java
/*
|
|
* This file is part of WechatTicketSystem.
|
|
*
|
|
* WechatTicketSystem is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* WechatTicketSystem is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with WechatTicketSystem. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package love.sola.netsupport.sql;
|
|
|
|
import com.google.common.cache.CacheBuilder;
|
|
import com.google.common.cache.CacheLoader;
|
|
import com.google.common.cache.LoadingCache;
|
|
|
|
import org.hibernate.Session;
|
|
import org.hibernate.criterion.Restrictions;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import love.sola.netsupport.config.Settings;
|
|
import love.sola.netsupport.pojo.User;
|
|
|
|
/**
|
|
* @author Sola {@literal <dev@sola.love>}
|
|
*/
|
|
@SuppressWarnings("Duplicates")
|
|
public class TableUser extends SQLCore {
|
|
|
|
public static final String COLUMN_ID = "id";
|
|
public static final String COLUMN_NAME = "name";
|
|
public static final String COLUMN_ISP = "isp";
|
|
public static final String COLUMN_NET_ACCOUNT = "netaccount";
|
|
public static final String COLUMN_WECHAT = "wechat";
|
|
public static final String COLUMN_BLOCK = "block";
|
|
public static final String COLUMN_ROOM = "room";
|
|
public static final String COLUMN_PHONE = "phone";
|
|
|
|
public static User getById(long id) {
|
|
try (Session s = sf.openSession()) {
|
|
return s.get(User.class, id);
|
|
}
|
|
}
|
|
|
|
public static User getByName(String name) {
|
|
try (Session s = sf.openSession()) {
|
|
return (User) s.createCriteria(User.class).add(Restrictions.eq(User.PROPERTY_NAME, name)).uniqueResult();
|
|
}
|
|
}
|
|
|
|
public static User getByWechat(String wechat) {
|
|
try {
|
|
User u = cache.get(wechat);
|
|
return u == NULL_USER ? null : u;
|
|
} catch (ExecutionException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static int update(User user) {
|
|
try (Session s = sf.openSession()) {
|
|
s.beginTransaction();
|
|
s.update(user);
|
|
s.getTransaction().commit();
|
|
cache.put(user.getWechatId(), user);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
protected static void init() {
|
|
try (Session s = SQLCore.sf.openSession()) {
|
|
User.OFFICIAL_CHINA_UNICOM_XH = s.get(User.class, 100104L);
|
|
User.OFFICIAL_CHINA_MOBILE_XH = s.get(User.class, 100864L);
|
|
User.OFFICIAL_CHINA_MOBILE_FX = s.get(User.class, 100865L);
|
|
}
|
|
}
|
|
|
|
private static LoadingCache<String, User> cache = CacheBuilder.newBuilder()
|
|
.concurrencyLevel(4)
|
|
.maximumSize(4096)
|
|
.expireAfterAccess(Settings.I.User_Wechat_Cache_Expire_Time, TimeUnit.SECONDS)
|
|
.build(new ValueLoader());
|
|
|
|
private static class ValueLoader extends CacheLoader<String, User> {
|
|
@Override
|
|
public User load(String key) throws Exception {
|
|
User u = TableUser.getByWechat0(key);
|
|
return u == null ? NULL_USER : u;
|
|
}
|
|
}
|
|
|
|
private static final User NULL_USER = new User();
|
|
|
|
public static void flushCache() {
|
|
cache.invalidateAll();
|
|
}
|
|
|
|
private static User getByWechat0(String wechat) {
|
|
try (Session s = sf.openSession()) {
|
|
return (User) s.createCriteria(User.class).add(Restrictions.eq(User.PROPERTY_WECHAT, wechat)).uniqueResult();
|
|
}
|
|
}
|
|
|
|
}
|