mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-12-13 22:00:29 +05:30
Attempt to optimize session handling. (#307)
This commit is contained in:
parent
e6c0f7c0f4
commit
d29d9c415d
@ -8,7 +8,6 @@ import me.kavin.piped.utils.*;
|
||||
import me.kavin.piped.utils.obj.db.PubSub;
|
||||
import me.kavin.piped.utils.obj.db.User;
|
||||
import me.kavin.piped.utils.obj.db.Video;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.StatelessSession;
|
||||
import org.schabi.newpipe.extractor.NewPipe;
|
||||
import org.schabi.newpipe.extractor.localization.Localization;
|
||||
@ -32,7 +31,7 @@ public class Main {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
System.out.println(String.format("ThrottlingCache: %o entries", YoutubeThrottlingDecrypter.getCacheSize()));
|
||||
System.out.printf("ThrottlingCache: %o entries%n", YoutubeThrottlingDecrypter.getCacheSize());
|
||||
YoutubeThrottlingDecrypter.clearCache();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@ -64,7 +63,7 @@ public class Main {
|
||||
.where(cb.and(
|
||||
cb.lessThan(root.get("subbedAt"), System.currentTimeMillis() - TimeUnit.DAYS.toMillis(4)),
|
||||
cb.isMember(root.get("id"), userRoot.<Collection<String>>get("subscribed_ids"))
|
||||
)).distinct(true);
|
||||
));
|
||||
|
||||
List<PubSub> pubSubList = s.createQuery(criteria).list();
|
||||
|
||||
@ -91,7 +90,7 @@ public class Main {
|
||||
new Timer().scheduleAtFixedRate(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
||||
|
||||
var cb = s.getCriteriaBuilder();
|
||||
var cd = cb.createCriteriaDelete(Video.class);
|
||||
@ -102,7 +101,7 @@ public class Main {
|
||||
|
||||
var query = s.createMutationQuery(cd);
|
||||
|
||||
System.out.println(String.format("Cleanup: Removed %o old videos", query.executeUpdate()));
|
||||
System.out.printf("Cleanup: Removed %o old videos%n", query.executeUpdate());
|
||||
|
||||
tr.commit();
|
||||
|
||||
|
@ -15,8 +15,7 @@ public class DatabaseSessionFactory {
|
||||
|
||||
final Configuration configuration = new Configuration();
|
||||
|
||||
Constants.hibernateProperties.forEach((key, value) -> configuration.setProperty(key, value));
|
||||
configuration.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
|
||||
Constants.hibernateProperties.forEach(configuration::setProperty);
|
||||
configuration.configure();
|
||||
|
||||
sessionFactory = configuration.addAnnotatedClass(User.class).addAnnotatedClass(Channel.class)
|
||||
@ -24,7 +23,7 @@ public class DatabaseSessionFactory {
|
||||
.addAnnotatedClass(PlaylistVideo.class).buildSessionFactory();
|
||||
}
|
||||
|
||||
public static final Session createSession() {
|
||||
public static Session createSession() {
|
||||
return sessionFactory.openSession();
|
||||
}
|
||||
|
||||
|
@ -824,7 +824,7 @@ public class ResponseHelper {
|
||||
if (user != null) {
|
||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||
var tr = s.beginTransaction();
|
||||
s.createNativeQuery("delete from users_subscribed where subscriber = :id and channel = :channel")
|
||||
s.createNativeMutationQuery("delete from users_subscribed where subscriber = :id and channel = :channel")
|
||||
.setParameter("id", user.getId()).setParameter("channel", channelId).executeUpdate();
|
||||
tr.commit();
|
||||
return mapper.writeValueAsBytes(new AcceptedResponse());
|
||||
@ -966,7 +966,7 @@ public class ResponseHelper {
|
||||
if (user != null) {
|
||||
|
||||
Multithreading.runAsync(() -> {
|
||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
||||
if (override) {
|
||||
user.setSubscribed(Set.of(channelIds));
|
||||
} else {
|
||||
@ -976,7 +976,7 @@ public class ResponseHelper {
|
||||
|
||||
if (channelIds.length > 0) {
|
||||
var tr = s.beginTransaction();
|
||||
s.merge(user);
|
||||
s.update(user);
|
||||
tr.commit();
|
||||
}
|
||||
}
|
||||
@ -1295,7 +1295,7 @@ public class ResponseHelper {
|
||||
|
||||
public static String registeredBadgeRedirect() {
|
||||
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
||||
long registered = (Long) s.createQuery("select count(*) from User").uniqueResult();
|
||||
long registered = s.createQuery("select count(*) from User", Long.class).uniqueResult();
|
||||
|
||||
return String.format("https://img.shields.io/badge/Registered%%20Users-%s-blue", registered);
|
||||
}
|
||||
@ -1347,9 +1347,9 @@ public class ResponseHelper {
|
||||
video = new Video(info.getId(), info.getName(), info.getViewCount(), info.getDuration(),
|
||||
Math.max(infoTime, time), info.getThumbnailUrl(), channel);
|
||||
|
||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
||||
var tr = s.beginTransaction();
|
||||
s.persist(video);
|
||||
s.insert(video);
|
||||
tr.commit();
|
||||
}
|
||||
|
||||
@ -1364,7 +1364,7 @@ public class ResponseHelper {
|
||||
Video video = DatabaseHelper.getVideoFromId(id);
|
||||
|
||||
if (video != null) {
|
||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
||||
updateVideo(s, video, item.getViewCount(), item.getDuration(), item.getName());
|
||||
}
|
||||
} else if (addIfNotExistent) {
|
||||
@ -1383,7 +1383,7 @@ public class ResponseHelper {
|
||||
Video video = DatabaseHelper.getVideoFromId(id);
|
||||
|
||||
if (video != null) {
|
||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
||||
updateVideo(s, video, info.getViewCount(), info.getDuration(), info.getName());
|
||||
}
|
||||
} else {
|
||||
@ -1396,7 +1396,7 @@ public class ResponseHelper {
|
||||
});
|
||||
}
|
||||
|
||||
private static void updateVideo(Session s, Video video, long views, long duration, String title) {
|
||||
private static void updateVideo(StatelessSession s, Video video, long views, long duration, String title) {
|
||||
|
||||
boolean changed = false;
|
||||
|
||||
@ -1415,7 +1415,7 @@ public class ResponseHelper {
|
||||
|
||||
if (changed) {
|
||||
var tr = s.beginTransaction();
|
||||
s.merge(video);
|
||||
s.update(video);
|
||||
tr.commit();
|
||||
}
|
||||
}
|
||||
@ -1490,14 +1490,14 @@ public class ResponseHelper {
|
||||
.build()).execute();
|
||||
|
||||
if (resp.code() == 202) {
|
||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
|
||||
var tr = s.beginTransaction();
|
||||
if (pubsub == null) {
|
||||
pubsub = new PubSub(channelId, System.currentTimeMillis());
|
||||
s.persist(pubsub);
|
||||
s.insert(pubsub);
|
||||
} else {
|
||||
pubsub.setSubbedAt(System.currentTimeMillis());
|
||||
s.merge(pubsub);
|
||||
s.update(pubsub);
|
||||
}
|
||||
tr.commit();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user