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