A man from California with a history of mental illness was sentenced Thursday to four years in federal prison for setting a fire last year inside the Comet Ping Pong pizza shop in Northwest Washington.

Ryan Jaselskis, 23, pleaded guilty in December to arson and assault on a police officer, a charge stemming from a fight with a U.S. Park Police officer who arrested him at the Washington Monument.

U.S. District Judge Timothy J. Kelly in Washington accepted the sentence that was agreed to ahead of time by prosecutors and defense attorneys.

In delivering his ruling, Kelly noted that the popular restaurant that features ping-pong tables had been targeted in an unrelated attack in 2016, when Edgar Maddison Welch entered with an AR-15 rifle seeking to investigate a viral Internet rumor of a child sex ring in the basement of the Chevy Chase eatery.

The judge said the fire set against that backdrop made ­Jaselskis’s crime “that much more damaging.”

While authorities have not been able to provide a specific motive behind Jaselskis’s attack, prosecutors said his sentence should match the four-year sentence that Welch received. Assistant U.S. Attorneys Dineen A. Baker and Andrew Floyd wrote in court papers that the cases are “similarly situated.”

Jaselskis’s attorneys with the federal public defender’s office drew contrasts with Welch, saying their client wasn’t drawn to Comet Ping Pong with a mission but rather was “suffering from a diagnosed mental illness.”

Thursday’s sentencing brings closure to another bizarre chapter at the Connecticut Avenue pizza shop. In 2016, it was thrust into the national spotlight when it became the subject of an Internet conspiracy theory known as “Pizzagate” that falsely asserted children were being held in tunnels under the restaurant.

When Welch was sentenced to prison, Comet Ping Pong owner James Alefantis told the court that he hoped the incident would one day be remembered as an aberration when “lies were seen as real and our social fabric had frayed.”

Three years later, Alefantis’s restaurant was targeted again, this time with a fire.

“My cooks worked through the experience of a gunman, and then they worked through the experience of an arsonist,” Alefantis told the court Thursday. “There is a sense of security that will no longer exist.

“We have been consistently on guard for attacks,” he said, noting his staff has had to “live in fear of customers walking through the front door.”

Jaselskis’s attorney David Walker Bos said he couldn’t “imagine being in the shoes of Mr. Alefantis” but asked the restaurant owner to understand his client “was somebody who at the time was laboring under pretty severe mental health issues.”

Bos said: “Even today he’s not really sure what led him to Comet pizza that night. But he knows what he did was wrong.”

Jaselskis addressed the court by telling Alefantis how “so, so sorry” he was. “I am taking full responsibility for my actions,” he said. “It has never been in my nature to harm anyone or engage in violence. . . . It breaks my heart that I acted in such a manner that goes completely against my upbringing and values.”

The judge was in the courtroom for the hearing, but several others, including the defendant, his attorneys and Alefantis, participated by video or telephone.

Jaselskis admitted to walking into the pizza shop the night of Jan. 23, 2019, as patrons were eating and playing games. He passed through the dining area and game room, and he headed into a back bar that was closed at the time and is typically used for birthday parties and other events.

Surveillance video captured him crouching by a black curtain that covered a rear wall and dousing it with lighter fluid. His first attempt to light the curtain failed. He left the restaurant, and then returned and successfully set the curtain on fire before walking out.

A customer and two kitchen employees grabbed the burning curtain and put out the fire. ­Jaselskis was arrested two weeks later after he was in an unauthorized construction area at the Washington Monument, police said. During his arrest, he struck a Park Police officer, bloodying his nose.

Prosecutors said in court filings that Jaselskis was raised by a “supportive and loving family” but faced “significant childhood challenges.” The filings say he had “troubling episodes” in 2018 in which he “decided not to follow through with medically recommended treatment.”

Prosecutors said that while jailed in this case, Jaselskis has undergone mental health treatment but still struggles with substance abuse. They said he has “distinguished himself” at the jail by taking courses and exhibiting “academic and extracurricular excellence.”

Bos said that while in jail his client participated in the Georgetown University Prison Scholars program, taking a full load of college courses, including History of African American Political Thought, English Literature and Philosophy of Law and Personal Finance.

The court file includes numerous letters from professors, jail employees and counselors, including a teacher from Georgetown who described Jaselskis as a “very strong student who is a pleasure to have in class . . . [who] is exactly the kind of person who will be better prepared to succeed when he returns to society.”